Operations on C/C ++ files (fread () fwrite ())

Source: Internet
Author: User

Fread and fwrite Functions

1. Function Functions

Used to read and write a data block.

2. General call form

Fread (buffer, size, Count, FP );

Fwrite (buffer, size, Count, FP );

3. Description

(1) buffer: A pointer. For fread, It is the storage address for reading data. For fwrite, It is the address of the data to be output.

(2) size: the number of bytes to read and write;

(3) count: the number of data items of size bytes to be read and written;

(4) FP: file pointer.

Note: After completing the fwrite () operation, you must close the stream (fclose ());

2. After a read operation (fread () is completed, if the stream (fclose () is not closed, the pointer (File * FP) automatically moves the length of the previous read/write backward, if the stream is not closed and the next read operation is continued, the output is continued;

3 fprintf (): Input to the stream in the format. Its prototype is int fprintf (File * stream, const char * Format [, argument,...]); the method is the same as printf (), but it is not written to the console, but to the stream. Note that the returned value is the number of bytes written to the file for this operation. For example, int c = fprintf (FP, "% S % d % F", str1, str2, a, B); str1: 10 bytes; str2: 10 bytes;: 2 bytes; B: 8 bytes; C: 33, because a space is automatically added between different data during writing.

You must disable the file after it is used. Otherwise, the content cannot be correctly displayed. fwrite: Read the information of two students and store the information in the file using fwrite.

Fread: Use fread to read student information from the 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 % 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 ");
}
}

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.