Fscanf, fprintf, fgets, fputs, fread, and fwrite functions, fscanffputs

Source: Internet
Author: User

Fscanf, fprintf, fgets, fputs, fread, and fwrite functions, fscanffputs
1. fscanf function and fprintf function 1.1 fscanf Function

The fscanf function can only be input in the format of a text file. The fscanf function is similar to the scanf function, but the input object is the data of the text file on the disk. The function is called as follows:

Fscanf (File pointer, format control string, input item table );

For example, if the file pointer fp points to an opened text file, a and B are integer variables, then, the following statement reads two integers from the file indicated by fp into the variables a and B:

Fscanf (fp, "% d", & a, & B );

Note: The two integers in the file are separated by spaces (escape and carriage return.

Fscanf (stdin, "% d", & a, & B );

It is equivalent:

Scanf ("% d", & a, & B );

Because the file pointer stdin represents the terminal keyboard.

1.2 functions of fprintf

The fprintf function converts data in memory into corresponding characters in the format and outputs the data to text files in the form of ASCII code. The fprintf function is similar to the printf function, but the output content is stored in the text file of the disk in the format. The function is called as follows:

Fprintf (File pointer, format control string, output item table );

For example, if the file pointer fp points to an opened text file, x and y are integer variables, the following statement outputs the integers x and y in the % d format to the file specified by fp.

Fprintf (fp, "% d", x, y );

Note: To facilitate future reading, the two numbers should be separated by spaces. At the same time, it is best not to output additional strings for later reading convenience.

Fprintf (stdout, "% d", x, y );

It is equivalent:

Printf ("% d", x, y );

The file pointer stdout represents the terminal screen.

2. fgets and fputs functions 2.1 fgets Functions

The fgets function is used to read strings from files. The fgets function is called as follows:

Fgets (str, n, fp );

Here, fp is the file pointer, str is the starting address for storing strings, and n is an int variable. The function reads n-1 characters from the file indicated by fp and puts them in the space starting with str. If you have read a linefeed or an EOF when the number of characters is not fully read, The read operation ends. The final part of the string contains the linefeed. Therefore, when the fgets function is called, up to n-1 characters can be read. After reading, the system automatically adds '\ 0' at the end and returns the result using str as the function value.

2.2 fputs Function

The fputs function is used to output strings to files. The fputs function is called as follows:

Fputs (str, fp );

Here, fp is the file pointer; str is the string to be output, which can be a String constant, a pointer to a string, or a character array name that stores the string. When this function is used for output, '\ 0' at the end of the string is not output, nor is' \ n' automatically added '. The output success function value is a positive integer; otherwise, it is-1 (EOF ).

Note that when the function output string is called, the strings in the file are connected at the beginning and end, and there will be no spacing between them. To facilitate reading, when outputting a string, you must manually add a string such as "\ n.

3. fread and fwrite Functions

Fread and fwrite are used to read and write binary files respectively. Their calling methods are as follows:

Fread (buffer, size, count, fp );

Fwrite (buffer, size, count, fp );

Buffer is the pointer of the data block. For fread, it is the first address of the memory block and the input data is stored in the memory block. For fwrite, it is the starting address of the data block to be output. Size indicates the number of bytes of each data block. Count is used to specify the number of input or output data blocks (each data block has size bytes ).

For example, the following struct:

Struct st


Char num [8];

Float mk [5];

} Pers [30];

Assume that each element in the pers array contains the student's student ID and scores of the five courses, and assume that there are already values in the 30 elements in the pers array. The file pointer fp indicates that the file has been correctly opened, then, execute the following loop to output data from these 30 elements to the file indicated by fp.

For (I = 0; I <30; I ++)


Fwrite (& pers [I], sizeof (struct st), 1, fp );


In the above for loop, each fwrite function call is executed, the "1" data block specified by the third parameter is output starting from the & pers [I] address, each data block contains sizeof (struct st) bytes, that is, the value of a struct variable is output at a time.

You can also use the following steps to read the data of each student into the pers array one by one from the files created above. In this case, the file must be opened for "read.

I = 0;

Fread (& pers [I], sizeof (struct st), 1, fp );

While (! Feof (fp ))


I ++;

Fread (& pers [I], sizeof (struct st), 1, fp );


Related Article

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.