Random Read and write of files
The previous read and write to the file are sequential read and write, that is, read and write files can only start from scratch, in order to read and write the data. However, in practical problems, it is often required to read and write only a specified part of a file. To solve this problem, you can move the position pointer within the file to the location where you want to read and write, and then read and write, which is called Random Read and write. The key to realizing random reading and writing is to move the position pointer as required, which is called file positioning. There are two functions in the file to locate and move the internal position pointer of the file, namely the rewind function and the fseek function.
The rewind function has been used several times before, and its invocation is in the form of: Rewind (file pointer), and its function is to move the position pointer inside the file to the top of the file. The following main introduction
fseek function.
The Fseek function is used to move the file's internal position pointer in the form of: fseek (file pointer, displacement, starting point) Where: The file pointer points to the file being moved. The amount of displacement represents the number of bytes moved, requiring that the displacement be long, so that no error occurs when the file length is greater than 64KB. When a constant is used to represent the displacement, the suffix "L" is required. Start point indicates where the displacement is calculated, with three types of starting point: File head, current position, and end of file.
The representation method is shown in Table 10.2.
The starting point represents a symbolic number representation
──────────────────────────
File First Seek-set 0
Current position Seek-cur 1
End of File Seek-end 2
For example:
Fseek (fp,100l,0); the significance is to move the position pointer to the first 100 bytes from the file. Also note that the Fseek function is typically used in binary files. In a text file, because of the conversion, there are often errors in the computed position. Random reading and writing of files after moving the position pointer, you can read and write with any of the read and write functions described earlier. Because it is generally read and write a data block, it is common to fread and fwrite functions. The following use case questions describe the random reading and writing of files.
[Example 10.8] Read the second student's data in the student file Stu list.
#include <stdio.h>
struct STU
{
Char name[10];
int num;
int age;
Char addr[15];
}BOY,*QQ;
Main ()
{
FILE *FP;
Char ch;
int i=1;
qq=&boy;
if ((Fp=fopen ("Stu_list", "RB")) ==null)
{
printf ("Cannot open file strike any key exit!");
Getch ();
Exit (1);
}
Rewind (FP);
Fseek (fp,i*sizeof (struct Stu), 0);
Fread (qq,sizeof (struct stu), 1,FP);
printf ("\n\nname\tnumber age addr\n");
printf ("%s\t%5d%7d%s\n", Qq->name,qq->num,qq->age,
QQ->ADDR);
}
The file stu_list has been established by the program of example 10.6, and this program reads the second student's data in a random readout method. The program defines boy as the STU type variable, and QQ is the pointer to the boy. Opens the file as a read binary file and moves the file position pointer on line 22nd of the program. The I value is 1, which means that the length of a Stu type is moved from the file header, and then the data read out is the second student's data.