Detailed explanation of "the usage of C language fread and fwrite" in the review of computer science upgraded course (first draft)

Detailed explanation of the usage of C language fread and fwrite (read and write files in the form of data blocks)

The fgets() function is "extreme", and can only "read one line of content in the file" at most at a time, because fgtes() ends reading when it encounters a "newline character".

If you want to read multiple lines of content, you need to use the fread() function. The corresponding write function is fwirte()


 

For Windows systems, the file should be opened as "binary" when using the freead() function and fwrite() function.


 

The fread() function is used to specify that "block data" is read from the file.

The so-called "fast data" refers to several "bytes" of data, which can be "a character" or "a string" or "multiple lines of data". There is no restriction.

The prototype of the fread() function is:

    size_t fread(void *ptr,size_t size,size_t count,FILE *fp);

The fwrite() function is used to "write" fast data to a file

Its prototype is:

    size_fwrite(void *prt,size_t size,size_t count,FILE *fp);

Description of parameters:

1. prt is the "pointer" of the memory area, which can be "array, variable, structure", etc.

ptr in fread() is used to store the "read" data, and prt in fwrite() is used to store the "write" data.

2. size: indicates the "number of bytes" of each data block

3. count: indicates the "number of blocks" of the data block to be "read / write"

4. fp: indicates the file pointer

5. Theoretically, read and write size*count bytes of data each time

size_t is in stdio H and stdlib The data type defined by typedef in the H header file represents an unsigned integer, that is, a non negative number, which is often used to represent quantity.


 

Return value:

When successful, the number of read-write blocks (i.e. count) is returned

If the return value is less than count:

For the fwrite() function, a write error must have occurred, which can be detected by the ferror() function.

For the free() function, "may" read the "end of file" or "error" may occur, which can be detected with "ferror()" or "feof()"

Write a program to deepen your understanding.

//eg: input an array from the keyboard, write the array to the file, and then read it out.

#include<stdio.h>

#define N 5

int main()

{

    //The data input from the keyboard is put into a, and the data read from the file is put into b

    int a[N],b[N];

    int i,size = sizeof(int);

    FILE *fp;



    if((fp=fopen("D:\\demo.txt","rb+")) == NULL)

    {

        puts("Fail to open file!");

        exit(0);

    }



    //Enter data from the keyboard and save it to array a

    for(i=0;i<N;i++)

    {

        scanf("%d",&a[i]);

    }



    //Writes the memory of array a to a file

    fwrite(a,size,Nfp);

   

    //Reposition the position pointer in the file to the beginning

    rewind(fp);



    //Read the memory from the file and save it to the array b

    fread(b,size,N,fp);



    //Display the contents of array b on the screen

    for(i=0;i<N;i++)

    {

        printf("%d",b[i]);

    }

    printf("\n");

}

result:

50 444 666 111 580↙

50 444 666 111 580


 

Open D: \ \ demo Txt and found that the contents of the file could not be read at all. This is because we use "rb +" to open the file, and the array will be written to the file in binary form intact, which is generally unreadable.

After the data is written, the position pointer is at the end of the file. To read the data, you must move the file pointer to the beginning of the file, which is rewind(fp); The role of.

//eg2 input the data of two students from the keyboard, write it into a file, and then read the data of the two students and display it on the screen.

#include<stdio.h>

#define N 2

struct stu{

    char name[10]; //full name

    int num;  //Student number

    int age;  //Age

    float score;  //achievement

}boya[N], boyb[N], *pa, *pb;

int main(){

    FILE *fp;

    int i;

    pa = boya;

    pb = boyb;

    if( (fp=fopen("d:\\demo.txt", "wb+")) == NULL ){

        puts("Fail to open file!");

        exit(0);

    }

    //Enter data from the keyboard

    printf("Input data:\n");

    for(i=0; i<N; i++,pa++){

        scanf("%s %d %d %f",pa->name, &pa->num,&pa->age, &pa->score);

    }

    //Write data of array boya to file

    fwrite(boya, sizeof(struct stu), N, fp);

    //Resets the file pointer to the beginning of the file

    rewind(fp);

    //Read data from file and save to data boyb

    fread(boyb, sizeof(struct stu), N, fp);

    //Output data in array boyb

    for(i=0; i<N; i++,pb++){

        printf("%s  %d  %d  %f\n", pb->name, pb->num, pb->age, pb->score);

    }

    fclose(fp);

    return 0;

}

result:

Input data:

Tom 2 15 90.5↙

Hua 1 14 99↙

Tom  2  15  90.500000

Hua  1  14  99.000000

Keywords: C

Added by moret on Sun, 06 Feb 2022 23:49:35 +0200