fread function and fwrite function __ function

Source: Internet
Author: User
Tags fread

1. function function

used to read and write a block of data.

2. General calling Form

fread (BUFFER,SIZE,COUNT,FP);

fwrite (BUFFER,SIZE,COUNT,FP);

3. Notes

(1) Buffer: is a pointer, for fread, it is read into the data storage address. For Fwrite, the address of the data to be exported.

(2) Size: The number of bytes to read and write;

(3) Count: The number of bytes of data to be read and written;

(4) FP: file type pointer.

Note: 1 Completion of the fwrite () must be closed after the flow (fclose ());

2 after 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-write, does not close the stream, continues the next read operation, then the last output continues to output;

3 fprintf (): input to stream in format, its prototype is int fprintf (FILE *stream, const char *format[, argument, ...]); The usage is the same as printf (), but it is written to the stream instead of to the console. 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 byte; str2:10 byte; a:2 byte; B:8 Byte, C is 33, because a space is automatically added between different data at write time.

Be sure to close the file after it is used, or you will not be able to display the content correctly. Fwrite: Read two student information and use fwrite to file

Fread: Read the student information from the file with Fread.

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");
}
}

Beyond C + + original articles, reproduced please indicate the source and retain the original link

This article link: http://www.beyondc.cn/fread-function-and-the-fwrite-function.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.