Files Session 2
Files Session 2
Files
Concept of a file, Opening and Closing files, file input / output functions (standard library input / output
functions for text files)
FILE STRUCTURE:
✓ I/O functions available are similar to their console counterparts; scanf becomes fscanf, printf
becomes fprintf, etc., These functions read and write from file streams.
✓ As an example, the file stream structure FILE defined in the header file stdio.h in DOS is shown
below:
typedef struct
{
int level; /* fill/empty level of buffer */
usigned flasgs; /* File status flags */
char fd; /* File descriptor (handle) */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char _FAR *buffer; /* Data transfer buffer */
unsigned char _FAR *curp;/* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE Object *
NOTE: FILE is defined as New Structure Data Type in the stdio.h file as shown in above method.
End Of File:
✓ EOF is a macro defined as an int with a negative value. It is normally returned by functions that
perform read operations to denote either an error or end of input.
✓ Input from a terminal never really "ends" (unless the device is disconnected), but it is useful to
enter more than one "file" into a terminal, so a key sequence is reserved to indicate end of input.
✓ Cntrl+Z is the key in DOS to end or terminate the input values.
✓ Cntrl+D is the key in UNIX to end or terminate the input values.
Example: /* A program to Write a file and read a file */
#include<stdio.h>
main( )
{
FILE *fp;
char ch;
Output:
Enter the Text:
Hi Raju...
how r u... how is ur studies...:-)
✓ fgetc( ) or getc( ) is a predefined file handling function which is used to read a single character
from a existing file opened in read(“r”) mode by fopen( ), which is same as like getchar( )
function.
Syntax:
ch_var= fgetc(filepointer); (Or) ch_var=getc(filepointer);
Example:
char ch;
ch=fgetc(fp); (Or) ch=getc(fp);
Where ‘ch’ is a character variable to be written to the file.
Where ‘fp’ is a file pointer object.
Output:
Text from file is:
Syntax:
fputc(ch_var , filepointer); (Or) putc(ch_var , filepointer);
Example: /* A program to Write a Character in a file and read a Character from a file */
#include<stdio.h>
main( )
{
FILE *fp;
Output:
Enter the Text:
Use Ctrl+z to stop entry
Hi ...
how are you... how is your
studies...:-)
^Z // Cntrl+Z key terminated the input values
Syntax:
4 Department of Computer Science and Engineering
Problem Solving and Programming with C GITAM
char *fgets(char *s, int n,FILE *fp);
or
Example:
char ch[10];
fgets(ch ,20, fp);
Where ‘ch’ is a string variable to be written to the file.
✓ fputs( ) is a predefined file handling function which is used to print a string in a new file opened in
write(“w”) mode by fopen( ), which is same as like puts( ) function.
Syntax:
int fputs(const char *s, FILE *fp);
or
fputs(ch_var , filepointer);
Example:
char ch[10]=”gitam”;
fputs(ch, fp);
Where ‘ch’ is a string variable.
Where ‘fp’ is a file pointer object.
✓ The function fputs( ) writes to the stream fp except the terminating null character of string s, it returns
EOF if an error occurs during output otherwise it returns a non negative value.
✓ The getw( ) and putw( ) are predefined file handling integer-oriented functions. They are similar to the
getc( ) and putc( ) functions and are used to read and write integer values.
✓ These functions would be useful when we deal with only integer data.
Enter a number: 16
From File int val=16
Output2:
Enter a number: 25 35 45
From File int val=25 /* here it take only one value, basing on program
requirement */
Output3:
Output4:
Enter a number: A
Frm File int val=28056 /* here it doesn’t print ASCII value of ‘A’, it just print
some-garbage value */
#include<stdio.h>
Output:
Enter the source file name apple.txt
Enter the new file name orange.txt
Vizag Zoo cell: 984822338File copied into orange.txt succesfull
/*Program to read data from input file and place first 10 characters
in an array and display on the monitor.*/
#include<stdio.h>
main()
{
FILE *fp;
int i=0;
char c,a[11];
fp=fopen(“INPUT”,”r”);
while(i<100)
{
c=getc(fp);
a[i]=c;
i++;
}
fclose(fp);
a[i]=’\0’;
puts(a);
}
Output:
Computer i
#include<stdio.h>
main()
Output:
Number of Statements terminators=10
No.of Opening braces=3
#include<stdio.h>
main()
{
#include<stdio.h>
#include<conio.h>
main()
#include<stdio.h>
main()