Fgets () has limitations that can only read one line of content from a file at a time, because fgets ends up reading when it encounters a newline character. If you want to read more than a line of content, you need to use the Fread function, and write the function as fwrite accordingly.
The fread () function is used to read block data from the specified file. the so-called block data, which is a number of bytes of data, can be a character, can be a string, can be multiple lines of data, and there is no limit. The prototype for Fread () is:
size_t fread (void *ptr, size_t size, size_t count, FILE *FP);
The fwrite () function is used to write block data to a file, and its prototype is:
size_t fwrite (void * ptr, size_t size, size_t count, FILE *FP);
Description of the parameter:
A PTR is a pointer to a block of memory that can be an array, a variable, a struct, and so on. The PTR in Fread () is used to hold the read data, and the PTR in Fwrite () is used to hold the data to be written.
size: Represents the number of bytes per block of data.
Count: The number of blocks that represent the blocks of data to read and write.
FP: Represents a file pointer.
Theoretically, each read and write Size*count byte of data.
size_t is the data type defined by typedef in the Stddef.h header file, representing unsigned integers, or non-negative numbers, that are commonly used to represent quantities.
Return value: Returns the number of blocks that were successfully read or written, or count. If the return value is less than count:
For Fwrite (), there must be a write error, which can be detected using the Ferror () function.
For Fread (), you may have read the end of the file, an error may have occurred, and can be detected with ferror () or feof ().
"Example" enters an array from the keyboard, writes the array to a file, and reads it.
#include <stdio.h>
#define N 5
int main () {
//input data from the keyboard into a, the data read from the file into B
int a[n], b[n];
int i, size = sizeof (int);
FILE *FP;
if ((Fp=fopen ("D:\\demo.txt", "rb+")) = = NULL) {
printf ("Cannot open file, press any key to exit!\n");
Getch ();
Exit (1);
}
Enter data from the keyboard and save to array a for
(i=0; i<n; i++) {
scanf ("%d", &a[i])
;
Writes the contents of array A to the file
fwrite (A, size, N, FP);
Relocate the position pointer in the file to the beginning of the file
Rewind (FP);
Reads content from a file and saves it to array B
fread (b, size, N, FP);
Displays the contents of array B on the screen for
(i=0 i<n; i++) {
printf ("%d", B[i]);
printf ("\ n");
Fclose (FP);
return 0;
}
Run Result:
23 409 500 100 222↙
23 409 500 100 222
The fwrite ()/fread () function directly manipulate bytes, and it is recommended that you open the file in binary mode. Read the difference between a text file in C and a binary file for more information.
Open D:\\demo.txt and find that the contents of the file cannot be read at all. This is because we use the "rb+" way to open the file, the data is written in binary form, generally unreadable.
After the data is written, the position pointer is at the end of the file, and to read the data, the file pointer must be moved to the beginning of the file, which is the role of Rewind (FP). For more information on the rewind function, please click: C Language Rewind function.
The suffix of the file is not necessarily. txt, it can be arbitrary, you can name it yourself, such as DEMO.DDD, Demo.doc, Demo.diy, etc.
The example enters two student data from the keyboard, writes to a file, and then reads the data from the two students on the screen.
#include <stdio.h>
#define N 2
struct stu{
char name[10];//name
int num;//School number
int age;
float score//Results
} Boya[n], Boyb[n], *pa, *PB;
int main () {
FILE *fp;
int i;
PA = Boya;
PB = Boyb;
if ((Fp=fopen ("D:\\demo.txt", "wb+")) = = NULL) {
printf ("Cannot open file, press any key to exit!\n");
Getch ();
Exit (1);
}
Input 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) ;
}
Writes the data of the array Boya to the file
fwrite (Boya, sizeof (struct Stu), N, FP);
Resets the file pointer to the beginning of the file
Rewind (FP);
Reads data from a file and saves it to the data boyb
fread (boyb, sizeof (struct Stu), N, FP);
Data in output array boyb for
(i=0; i<n; i++,pb++) {
printf ("%s%d%d%f\n", Pb->name, Pb->num, Pb->age, PB-&G T;score);
}
Fclose (FP);
return 0;
}
Run Result:
Input Data:
Tom 2 15 90.5↙
Hua 1 14 99↙
Tom 2 15 90.500000
Hua 1 14 99.000000
The above is the C language to read block form of the example code, hoping to help learn this part of the students.