1. fscanf function and fprintf function 1.1 fscanf function
The FSCANF function can only be entered as a format from a text file. The FSCANF function is similar to the scanf function, except that the input object is the data for the text file on disk. The function is called as follows:
FSCANF (file pointer, format control string, input table);
For example, if the file pointer fp points to an open text file, A and B are integer variables, the following statement reads two integers into variables a and b from the file that the FP refers to:
FSCANF (FP, "%d%d", &a, &b);
Note: The two integers in the file are separated by a space (jump Geff, carriage return).
FSCANF (stdin, "%d%d", &a, &b);
Equivalent to:
scanf ("%d%d", &a, &b);
Because the file pointer stdin represents the terminal keyboard.
1.2 fprintf function
The fprintf function converts the in-memory data into corresponding characters in a format and outputs it to a text file as an ASCII code. The fprintf function is similar to the printf function, except that the output will be formatted in a text file on the disk as well. The function is called as follows:
fprintf (file pointer, format control string, output Item table);
For example, if the file pointer fp points to an open text file, and x and y are integer variables, the following statement outputs the integers in the x and y two integer variables in%d format to the file referred to in the FP.
fprintf (FP, "%d%d", x, y);
Note: For later reading, two numbers should be separated by a space. It is also best not to output additional strings for later readability.
fprintf (stdout, "%d%d", x, y);
Equivalent to:
printf ("%d%d", x, y);
The file pointer stdout represents the terminal screen.
2. fgets function and fputs function 2.1 fgets function
The Fgets function is used to read a string from a file. The Fgets function is called in the following form:
Fgets (str, n, FP);
Here, the FP is the file pointer, Str is the starting address for the string, and n is an int variable. The function is to read n-1 characters from the FP file into a space with the starting address of Str. If a newline character or an EOF (end-of-file flag) is read when the n-1 characters are not read, the read is ended, and the last read of the string is read into the line feed. Therefore, when you call the Fgets function, you can read a maximum of n-1 characters. At the end of the read, the system will automatically add ' + ' at the end and return with STR as the function value.
2.2 fputs function
The Fputs function is used to output a string to a file. The Fputs function is called in the following form:
Fputs (str, FP);
Here, the FP is the file pointer; Str is the string to be output, which can be a string constant, a pointer to a string, or a character array name that holds a string. When using this function for output, the last ' \ ' in the string is not output and does not automatically add ' \ n '. The output Success function value is a positive integer, otherwise-1 (EOF).
It is important to note that when the function output string is called, the strings in the file are first-to-end, and there will be no spacers between them. In order to be readable, you should pay attention to artificially adding strings such as "\ n" when outputting strings.
3. Fread functions and Fwrite functions
The Fread function and the fwrite function are used to read and write binary files, respectively. They are called in the following form:
Fread (buffer, size, count, FP);
fwrite (buffer, size, count, FP);
Where buffer is a pointer to a block of data, for Fread, it is the first address of a block of memory, the input data is stored in this memory block, and for fwrite, it is the starting address of the data block that is ready to be output. A size represents the number of bytes per data block. Count is used to specify the number of data blocks to read, write, or output (each chunk has a size byte).
For example, there are the following structures:
Struct ST
{
Char Num[8];
float mk[5];
} pers[30];
Assuming that each element of the Pers array contains the student's number and the scores of the five courses, and assumes that the 30 elements of the PERS array already have values, the file pointer fp refers to a file that has been opened correctly, the following loop will output the data from the 30 elements to the file referred to in the FP.
for (i = 0; i <; i++)
{
Fwrite (&pers[i], sizeof (struct st), 1, FP);
}
In the For loop, each time the Fwrite function call is executed, the "1" data block specified by the third parameter is output from the &pers[i] address, each containing the sizeof (struct ST) byte, which is the value in one structure variable at a time.
You can also use the following steps to read each student's data into the pers array again from the file created above. At this point, the file must be open for "read".
i = 0;
Fread (&pers[i], sizeof (struct st), 1, FP);
while (!feof (FP))
{
i++;
Fread (&pers[i], sizeof (struct st), 1, FP);
}
FSCANF functions and fprintf functions, fgets functions and fputs functions, fread functions, and fwrite functions