Input output functions that were declared in earlier sections were quite useful, but in addition to that we can use two more functions - "fprintf()" and "fscanf()". As the names suggest both the functions are similar to "printf()" and "scanf()" which are basic Input/Output functions. The difference is that "fprintf()" and "fscanf()" work with files only. This function is similar to printf(), except that a FILE pointer is included as the first argument. As in printf( ), we can format the data in a variety of ways, by using fprintf( ). In fact all the format conventions of printf( ) function work with fprintf( ) as well.
Syntax :
int fprintf(FILE *fp, const char *control_string, . . .);
int fscanf(FILE *fp, const char *control_string, . . .);
Although fprintf() and fscanf() often are the easiest way to write and read formatted data to disk files, they are not always the most efficient. Because formatted ASCII data is being written as it would appear on the screen (instead of in binary), extra overhead is incurred with each call. Therefore to run the program speedily we should use fread() and fwrite().
Example 1 : Using fprintf() and fscanf()
#include
#include
#include
void main()
{
FILE *fp; // Line 1
char str[30]; // Line 2
int t; // Line 3
if((fp=fopen("myfile", "w")) == NULL) // Line 4
{ // Line 5
printf(''File Can Not Be Created !!"); // Line 6
exit(1); // Line 7
} // Line 8
printf("Enter some text and a digit "); // Line 9
fscanf(stdin, "%s%d", s, &t); // Line 10
fprintf(fp, "%s %d", s, t); // Line 11
fclose(fp) // Line 12
if((fp=fopen("test","r")) == NULL) // Line 13
{ // Line 14
printf("Error in opening file"); // Line 15
exit(1); // Line 16
} // Line 17
fscanf(fp, "%s%d", s, &t); //Line 18 -- Reading data from the file in %s(string) and %d(int) format
fprintf(stdout, "%s %d", s, t); // Line 19 -- Printing the data on the screen.
}
Description
The above program reads a string and an integer from the keyboard and writes them to a file called MYFILE. The program then reads the file and displays the information on the screen. After running this program, examine the MYFILE.
Line 4,6 and 7
Tries to create/open a file in "write(w)" mode. If due to any reason the file can not be created, the "fopen()" function will return NULL. The program will exit.message after displaying the message "File can not be created".
Line 9
Will ask the user to enter some text and a digit.
Line 10
The "fscanf()" function will accept the data in %s and %d format.
Line 11
The "fprintf()" will "print" (write) both the values to the file. The first argument is the file pointer to which we want to write the data, where as the second and third arguments are the formats in which we want to write the data.
Line 12
Will close the file from "writing" mode.
Line 13
To read the file, the file is opened using "fread()". If the system is unable to open the file in read mode the file pointer will again return NULL and the program will exit again after printing the message "Error in opening file".
Line 18
If the file opens sucessfully, the fscanf() function will read the data from the file. Note that the format in which the data is to be read is specified again.
Line 19
The read data is printed on screen. The first argument "stdout" specifies that the data is to be printed to the "Standard Output" device which is "monitor".
0 comments:
Post a Comment