The C standard library provides two functions that facilitate line input and output with files
fgets(line, maxline, file pointer);
fputs (line, file pointer) ;
where line is a character array, and maxline is the maximum no. of characters that might be read. fget s reads the next input line including the newline character ' ' into the character array line (at most maxline - 1 characters will be read). Normally it returns line, on end of file or error it returns NULL, fputs writes line into the file and returns zero, i1 a error occurs it returns EOF. The following program illustrates the use of the functions fgets and fputs –
Example 7.3
/* let us take the example of the file copy program given in example 7 .2 and rewrite it using fgets and fputs */
#include <stdio.h>
void main()
{
char line[251];
FILE * fp1, * fp2;
fp1 = fopen ("exam1.dat", "r");
fp2 = fopen( "exam2. dat", "w");
while (fgets (line, 251,fp1) ! =NULL)
{
fputs (1ine, fp2);
printf ( "
%s", line) ;
}
fclose (fp1);
fclose (fp2);
}
Note that in the fgets Statement, the maximum line length is specified as 251. So it reads 251-1 characters at a stretch into line and adds a "" at the end of the string. If, however, a ' ' is encountered before that, a '' is added at the end of the string. While reading, if the end of file is reached, fgets returns a NULL.
Formatted /O with files
Assume that in a file exam.dat, the file has to be read one word at a time, printed on the screen and at the end of the read operation, the number of words in the file must be displayed on the screen. The functions –
fprintf (fp, format, …….) &
fscanf (fp, format, …….)
facilitate this. These functions are very similar to printf and scanf but the additional parameter fp is a file pointer, which specifies the file that is to be read from or written into.The second parameter is a character string which specifies the format.
Example 7.4
#include <stdio.h>
void main( )
{
int wcount=0;
char str[20];
FILE *fp;
fp = fopen("exam.dat", "r");
while (fscanf ( fp, "e",str) != EOF)
{
wcount++;
fprintf (stdout,
%s", str) ; /* since stdout is treated as just another file, fprintf can also be used to print messages on the screen */
fprintf (stdout, " %d", wcount);
}
printf("
The number of words in the program is %d", wcount);
}
Random access using fseek()
Whenever an input or an output is performed on a file, it results in the pointer being shifted to the next "'position" in the file. The "current position" is the position at which data will be read from in an input operation or written to in an output operation. When a file is opened, the "current position", or offset is always at the beginning of the file. The C standard library provides a function that facilitates random shifting of the pointer "position" or offset –
fseek (fp, offset, from-where);
where, fp is the file pointer, offset is a long int that specifies the number of bytes that have to be shifted, and from where is literally from where- it is an integer specifying the position on the file from where the offset would be effective. The three values it can take are
0- offset is effective from beginning of the file
1 - offset is effective from the current position
2- offset is effective from the end of the file
fseek returns 0 if successful and 1 if unsuccessful. Figure 6.1 to 6.3 illustrate how the function fseek affects the shift in current position on a file
fp = fopen ("exam. dat ", "r");
fseek (fp, 0,0); /* shift zero bytes from beginning of the file, this statement is actually inessential - opened, as soon as a file is opened, the file pointer is always positioned at the beginning of the file */
current pointer position (when a file is opened the current position is always 1)
fseek (fp, 5,0) /* shift 5 bytes from the beginning of the file */
the current position has been shifted 5 bytes from the beginning of the file
fseek ( fp, -2,1) /* shift -2 bytes from the current position */
Current position shifted -2 bytes
The ftell () function
This function returns the current offset position in the file, for example., as soon as a file is opened, this function returns 1.
long int position = ftell (fp);
The rewind () function
This function enables repositioning of the pointer at the beginning of the file
rewind(file-pointer) ;
After a rewind is performed, the current position becomes 1.