1. function function
Used to read and write a data block.
2. General Invocation Form
Fread (BUFFER,SIZE,COUNT,FP);
Fwrite (BUFFER,SIZE,COUNT,FP);
3. Description
(1) Buffer: is a pointer to Fread, which is the storage address of the read-in data. For Fwrite, the address of the data to be output.
(2) Size: The number of bytes to read and write;
(3) Count: The number of bytes of data to read and write;
(4) FP: file-type pointer.
Note: 1 must close the stream (Fclose ()) After completion of the write operation (fwrite ());
2 Once a read operation (Fread ()) is completed, if the stream is not closed (fclose ()), then the pointer (FILE * FP) automatically moves backwards the length of the previous read and write, does not close the stream to continue the next read operation, then the last output continues to output;
3 fprintf (): entered into the stream by format, its prototype is int fprintf (FILE *stream, const char *format[, argument, ...]); The usage is the same as printf (), but it's not written to the console, but to the stream. Note that the return value is the number of bytes written to the file for this operation. such as int c = fprintf (FP, "%s%s%d%f", STR1,STR2, A, b), str1:10 bytes, str2:10 bytes, a:2 bytes, b:8 bytes, C is 33, because a space is automatically added between different data when writing.
The file must be closed after it is used, otherwise the content will not be displayed correctly. Fwrite: Read two student information and then deposit it with fwrite
Fread: Use Fread to read student information from a file.
Fwrite.c
#include <stdio.h>
#define SIZE 2
struct Student_type
{
Char name[10];
int num;
int age;
Char addr[10];
}stud[size];
void Save ()
{
FILE *FP;
int i;
if ((Fp=fopen ("Stu_list", "WB")) ==null)
{
printf ("Cant open the file");
Exit (0);
}
for (i=0;i<size;i++)
{
if (fwrite (&stud[i],sizeof (struct student_type), 1,FP)!=1)
printf ("File Write error\n");
}
Fclose (FP);
}
Main ()
{
int i;
for (i=0;i<size;i++)
{
scanf ("%s%d%d%s", &stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
Save ();
}
for (i=0;i<size;i++)
{
printf ("%s,%d,%d", stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
}
Fread.c
#include <stdio.h>
#define SIZE 2
struct Student_type
{
Char name[10];
int num;
int age;
Char addr[10];
}stud[size];
void Read ()
{
FILE *FP;
int i;
if ((Fp=fopen ("Stu_list", "RB")) ==null)
{
printf ("Cant open the file");
Exit (0);
}
for (i=0;i<size;i++)
{
if (fread (&stud[i],sizeof (struct student_type), 1,FP)!=1)
printf ("File Write error\n");
}
Fclose (FP);
}
Main ()
{
int i;
Read ();
for (i=0;i<size;i++)
{
printf ("%s,%d,%d,%s", stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
printf ("\ n");
}
}
We often use fread () and fwrite () to read and write files in the C language when they are being manipulated. Here is a detailed look at the use of these two functions.
When we write programs in C, we generally use standard file systems, which are buffered file systems. The system opens a "file buffer" in memory for each file that is being read and written, and the data passes through the buffer when the file is read and written. To read and write to the file, the system first opens up a memory area to hold the file information, and the information is stored in a struct, and the structure typedef is the file type. We first define a pointer to the struct, and when the program opens a file, we get a pointer to the file structure, and with this pointer we can manipulate it. For example:
#i nclude <stdio.h>
#i nclude <string.h>
int main ()
{
FILE *FP;
Char buffer[100] = "This is a test";
if (fp = fopen ("C:\\example.txt", "w")) = = = 0)
{
printf ("Open failed!");
Exit (1);
}
fwrite (buffer, 1, strlen ("This is a Test"), FP);
Fclose (FP);
return 0;
}
With the above code, we set up a file named example with a. txt extension in the root directory of the C drive, and we open it to see that it is a test. When we read it out to it, use the following code:
#i nclude <stdio.h>
#i nclude <mem.h>
int main ()
{
FILE *FP; int Len;
Char buffer[100];
/*memset (buffer, 1, 100); */
if (fp = fopen ("C:\\example.txt", "r")) = = = 0)
{
printf ("Open failed!");
Exit (1);
}
Fseek (FP, 0L, seek_end);
Len = Ftell (FP);
Rewind (FP);
Fread (buffer, 1, Len, FP);
printf ("%s", buffer);
Fclose (FP);
Getch ();
return 0;
}
Can see, when we use memset, read out a lot of garbled, this is why? The reason is that the number of bytes that we write in the Fwrite function is calculated using strlen, which means that the last ' s ' of the string is not written to the file. So when we read the buffer from the file, we naturally don't have ' s ', because the number in buffer is random, unless the next number of the last character in buffer is exactly 0 (the probability is very small, it is excluded by memset), Otherwise, the characters in buffer will not be encountered when the%s is output to 0, so garbled. There are many ways to do this, and you can write more than one byte when writing to a file, and the system will automatically write 0,fwrite (buffer, 1, strlen ("This is a test") +1, FP); So there's a 0 at the end of the reading. Or, after the read operation completes, a 0:buffer[len] = 0 is appended to the last character, and the problem can be resolved.
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/greytree/archive/2006/06/02/769952.aspx
"Go" fread function and fwrite function