For example: Fread (fa, fp); it means that four bytes (a real number) are read each time from the file indicated by fp and sent to the real array fa for five consecutive reads, read 5 real numbers into fa. [Example 10.6] input two student data from the keyboard, write them into a file, and read the data of the two students on the screen. # Include <stdio. h> Struct stu { Char name [10]; Int num; Int age; Char addr [15]; } Boya [2], boyb [2], * pp, * qq; Main () { FILE * fp; Char ch; Int I; Pp = boya; Qq = boyb; If (fp = fopen ("stu_list", "wb +") = NULL) { Printf ("Cannot open file strike any key exit! "); Getch (); Exit (1 ); } Printf ("/ninput data/n "); For (I = 0; I <2; I ++, pp ++) Scanf ("% s % d % s", pp-> name, & pp-> num, & pp-> age, pp-> addr ); Pp = boya; Fwrite (pp, sizeof (struct stu), 2, fp ); Rewind (fp ); Fread (qq, sizeof (struct stu), 2, fp ); Printf ("/n/nname/tnumber age addr/n "); For (I = 0; I <2; I ++, qq ++) Printf ("% s/t % 5d % 7d % s/n", qq-> name, qq-> num, qq-> age, qq-> addr ); Fclose (fp ); } In this example, the program defines a structure stu, indicating two arrays boya and boyb, and two structure pointer variables pp and qq. Pp points to boya, qq points to boyb. Line 2 of the program opens the binary file "stu_list" in read/write mode, enters two student data, writes it to the file, and moves the internal position pointer of the file to the beginning of the file, after reading two pieces of student data, it is displayed on the screen.Formatting read/write functions fscanf and fprintf The fscanf function and fprintf function are similar to the functions of the scanf and printf functions used earlier. They are both formatted read/write functions. The difference between the fscanf function and fprintf function is that the Read and Write objects are not keyboard and display, but disk files. The call formats of these two functions are: fscanf (File pointer, Format String, input table column); fprintf (File pointer, Format String, output table column); for example: Fscanf (fp, "% d % s", & I, s ); Fprintf (fp, "% d % c", j, ch ); The fscanf and fprintf functions can also be used to solve the problem in the case of 10.6. Shows the modified program in example 10.7. [Example: 10.7] # Include <stdio. h> Struct stu { Char name [10]; Int num; Int age; Char addr [15]; } Boya [2], boyb [2], * pp, * qq; Main () { FILE * fp; Char ch; Int I; Pp = boya; Qq = boyb; If (fp = fopen ("stu_list", "wb +") = NULL) { Printf ("Cannot open file strike any key exit! "); Getch (); Exit (1 ); } Printf ("/ninput data/n "); For (I = 0; I <2; I ++, pp ++) Scanf ("% s % d % s", pp-> name, & pp-> num, & pp-> age, pp-> addr ); Pp = boya; For (I = 0; I <2; I ++, pp ++) Fprintf (fp, "% s % d % s/n", pp-> name, pp-> num, pp-> age, pp-> Addr ); Rewind (fp ); For (I = 0; I <2; I ++, qq ++) Fscanf (fp, "% s % d % s/n", qq-> name, & qq-> num, & qq-> age, qq-> addr ); Printf ("/n/nname/tnumber age addr/n "); Qq = boyb; For (I = 0; I <2; I ++, qq ++) Printf ("% s/t % 5d % 7d % s/n", qq-> name, qq-> num, qq-> age, Qq-> addr ); Fclose (fp ); } Compared with example 10.6, in this program, the fscanf and fprintf functions can read and write only one structural array element at a time. Therefore, they use loop statements to read and write all array elements. Note that the pointer variable pp, qq, has changed their values cyclically, so they are re-assigned the first address of the array in line 25 and 32 of the program. Random file read/write The read and write methods for files are sequential read and write, that is, the read and write files can only start from the beginning, read and write data sequentially. However, in practice, it is often required that a specified part of the read-only file be written. To solve this problem, the position pointer inside the file is moved to the location where the file needs to be read and written before reading and writing. This read and writing is called random read and write. The key to achieving random read/write is to move the location pointer as required, which is called file location. There are two main functions used to locate and move the internal position pointer of a file, namely the rewind function and the fseek function. The rewind function has been used multiple times before and is called in the form of rewind (File pointer). Its function is to move the position pointer inside the file to the beginning of the file. The following describes Fseek function. The fseek function is used to move the internal position pointer of a file. The call form is fseek (File pointer, displacement, starting point). The "file Pointer" points to the object to be moved. "Displacement" indicates the number of bytes to move. The displacement is long data, so that no error occurs when the file length is greater than 64 KB. When a constant is used to represent the displacement, the suffix "L" is required ". The "Starting Point" indicates where to start the calculation of the displacement. There are three defined starting points: the first part of the file, the current position, and the end of the file. |