Linux stream-based I/O

Source: Internet
Author: User

1. stream and Buffer

Stream I/O is provided by standard functions in C language. These I/O can replace the Read and Write functions provided in the system. In fact, the stream I/O encapsulates the two basic file read/write system calls. To some extent, it is easier to use stream I/O. These I/O functions are not significantly different in efficiency.

Stream-based operations will eventually call the read or write functions for operations. To enableProgramStreaming objects usually provide a buffer to reduce the number of calls to system I/O library functions.

Stream-based I/O provides the following two types of buffering:

1. Full Buffer: the system I/O function is called only when the buffer is full. For read operations, I/O operations are not performed until the number of bytes of the read content is equal to the buffer size or the file ends with the arrival of the object; for write operations, the actual I/O operation will not be performed until the buffer is filled up to write the buffer content to the external storage file. Disk Files are usually fully buffered.

2. Row buffering: the system I/O function is called only when the line break \ n is encountered. For read operations, the I/O operation is performed only when the linefeed \ n is used to write the read content to the buffer. For write operations, the I/O operation is performed only when the linefeed \ n is used, write the buffer content to the external store. The buffer size is limited, so when the buffer is filled up, even if \ n is not encountered, the actual I/O operation will also be performed. Both stdin and stdout are row-buffered by default.

3. No buffer: If there is no buffer, the data will be immediately read or output to external files and devices. Standard Error stderr is not buffered. This ensures that error prompts and outputs can be promptly reported to users for troubleshooting.

2. file stream-based operations

Common functions:

1. Open and Close the stream

# Include <stdio. h>

File * fopen (const char * restrict pathname, const char * restrict type );

File * fdopen (INT fileds, const char * type );

The first parameter of the fopen function indicates the path of the file to be opened, and the second parameter indicates the method of opening.

The fdopen function is used to create a stream in an opened file. The first parameter is the file descriptor of the opened file, and the second parameter is the same as the second parameter of the fopen function. The only difference is that because the file has been opened, the fdopen function will not create a new file, nor will it trunch the file to 0. This is a hot spot, these two points have been completed when the file descriptor is opened.

In Linux, use the fclose function to close a file stream. The function prototype is as follows:

# Include <stdio. h>

Int fclose (File * FP );

If the execution is successful, the function returns 0, and the Failure Returns EOF. This value is defined in stdio. h and its value is-1. When the fclose function closes a file, the function writes the file content that has not been written back to the disk in the memory to the disk. It is important to understand this. If the fclose function is not called, you must wait until the buffer in the memory is filled up and the system writes the content back to the disk. Many programmers are troubled by the need to check the return value of the fclose function. Although strictly speaking, all system call return values should be checked and error handling should be performed, the error probability of fclose function is very small, almost 0. however, if you close a remote file in a network environment, the fclose function may fail. Because the fclose function writes the buffer content back to the disk when closing the file, the fclose function actually performs a write operation. In the network environment, the file content must be transmitted to the target host over the network and written to the disk. During the transmission process, if the network link is faulty or the data transmission fails, the file content writing will fail. In this case, an error occurs in the fclose function. We can see that,If you close a file locally, you do not need to check the return value. If you close a file in the network environment, it is necessary to check the return value of the fclose function..

3. Read and Write Data in characters

The I/O mode for reading and writing a single character data each time is called the I/O mode for each character. In Linux, use the fgetc function to get a character. The function prototype is as follows:

# Include <stdio. h>

Int fgetc (File * FP );

If the function is successfully executed, the value of ascll is returned. If the execution fails, the value of EOF is returned.

In Linux, use the fputc function to output a character data. The function prototype is as follows:

# Include <stdio. h>

Int fputc (int c, file * FP)

The first parameter indicates the ascll value (source) of the character to be output, and the second parameter indicates the file stream (destination) to be output ).

4. Read and Write Data in behavior units

When the input content encounters \ n, the I/O method that sends the content before \ n in the stream to the buffer is called the I/O of each row. Linux uses the following functions to read a row at a time.

# Include <stdio. h>

Char * fgets (char * restrict Buf, int N, file * restrict FP );

Char * gets (char *);

The first parameter of the fgets function indicates the buffer to be read, and the second parameter n indicates the number of characters to be read. The maximum value of this parameter cannot exceed the length of the buffer. The fgets function keeps reading until \ n is encountered. If the line break is not encountered within n-1 characters, it is read-only into n-1 characters. The last character is used to store the string ending sign \ 0. note that the fgets function will also read the '\ n' line break into the buffer. Therefore, the actual valid content of the buffer should be the actual number of bytes in the buffer (excluding' \ 0 ') minus 1. the third parameter of the fgets function is the stream object to be read.

The fgets function has the following two return values: 1. A row is successfully read and the first address of the buffer is returned. 2. If an error occurs during reading or the file has reached the end, null is returned.

The gets function is similar to the fgets function. This function reads a row from the standard input stream and saves it to a buffer without reading '\ n' into the buffer. The Return Value of the gets function is the same as that of fgets.

In Linux, fputs and puts functions are used to output a line of strings. The function prototype is as follows:

# Include <stdio. h>

Int fputs (const char * restrict STR, file * restrict FP );

Int puts (const char * Str );

The first parameter of the fputs function indicates the buffer that stores the output content, and the second parameter indicates the file to be output. If the execution is successful, the number of output bytes is returned. If the execution fails,-1 is returned. The puts function outputs a line of string with the standard output. The parameter is the same as the first parameter of the fputs function. If the output is successful, the number of bytes is returned. If the output fails,-1 is returned, it is worth noting that,Although the gets function does not read \ n, the puts function outputs \ n.Both fputs and puts functions do not output the string Terminator '\ 0 '. For I/O, the combination of fputs and fgets functions is safe and reliable.

5. Gets Function Vulnerability

The biggest difference between the gets function and the fgets function is that although the buffer zone of the gets function is provided by the user, the user cannot specify the maximum number of bytes it can read at a time. This makes the gets function a dangerous function.

(the content in this article is from "Linux C Programming Daquan" Wu Yue)

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.