Reread the read functions in standard Io

Source: Internet
Author: User

Author: Wang Shanshan,Hua Qing vision embedded college lecturer.

In standard Io, there are only three methods to read and write files:

(1) I/O of each character.
(2) I/O of each row.
(3) Direct I/O.

It seems that these functions seem very simple, but only by understanding these functions can you know how to use them, for example, how to determine whether the read file is complete, how to count the number of lines of the file, and so on.

Next we will review the three methods for reading files, and then give an example of how to use them.

Use GETC, fgetc, or getchar to read or write one character at a time. If the stream is cached, the standard I/O function processes all the caches. The three function prototypes are as follows:

# Include <stdio. h>
Int GETC (File * FP );
Int fgetc (File * FP );
Int getchar (void );

Return of the three functions: if the operation succeeds, it is the next character. If the operation is completed at the end of the file or when an error occurs, it is e o f. It is emphasized that the three functions return the same value regardless of whether an error occurs or the end of the file is reached. To distinguish between the two cases, you must call ferror () or feof ().

Read or write a row at a time, using f g e t s and gets. The two function prototypes are as follows:
# Include <stdio. h>
Char * gets (char * s );
Char * fgets (char * s, int size, file * steam)

Two functions are returned: if the operation is successful, the value is Buf. If the operation is completed at the end of the file or when an error occurs, the value is null. Both functions specify the cache address, and the row to be read is sent to it. Gets () reads from the standard input, while fgets () reads from the specified stream.

For fgets (), the length of the cache must be specified as N. This function keeps reading the next new line character, but cannot exceed n-1 characters. The read characters are sent to the cache, And the cache ends with null characters. If the number of characters in the row including the last New Line Character exceeds n-1, only an incomplete row is returned, and the cache always ends with a null character. The next call to fgets () will continue to read the row.

Direct I/O uses fread. Each I/O operation reads a certain number of objects, and each object has a specified length. This function is often used to read a structure from a binary file. The prototype is as follows:
Int fread (void * PTR, int objsize, int objnum, file * FP );

This function returns the number of read objects. Fputs () stops when a NULL byte is encountered, and the structure may contain null bytes. Therefore, this requirement cannot be implemented using each row of function. fread allows us to read the entire structure at a time.

Next, let's take a look at how to use the standard Io READ function mentioned above to determine whether the file you are reading is complete and how to count the number of lines of the file.

1. There are three methods to determine the end of a file:

A) when the int value returned by fgetc (SRC) is EOF, read the end Of the file
B) when the pointer returned by fgets (p, 1024, Src) is null, read the end Of the file
C) when the number returned by fread (S, Src) is less than 20, read the end Of the file

The implementation is simple and clear, so we will not elaborate on it.

2. Number of statistics files

A) when the int value returned by fgetc (SRC) is '/N', the number of rows is incremented by one.
B) when the pointer returned by using fgets (p, 1024, Src) is not null, your buffer will provide P [1024] With read characters, in this case, there are actually two situations: one is that the length of the characters you read is less than 1024. At this time, one row ends, the number of rows increases by one, and the other is more complex, due to the limitation of self-defined buffering, only 1023 data can be read at a time. If you just read one row at a time, the P [] 1022] Must be '/N ', if not, your line is not over yet.

The procedure is as follows:
Int N;
A) while (fgetc (SRC) = '/N ')
N ++;
B) while (fgets (p, 1024, Src )! = NULL)
{
If (strlen (p) <1024 | P [1022] = '/N ')
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.