These days, I participated in the ZTE's monthly support competition and used the standard I/O Library of C language. Some of the details are not very clear, which leads to many bugs and takes a long time to debug. So take a note here for future reference.
1. Buffer Zone
Full buffer. In this case, the actual I/O operation is performed only after the standard I/O cache is filled. Access to files residing on the disk is usually fully buffered by the standard I/O library. The buffer can be automatically rinsed by standard IO routines (such as when a buffer is filled up), or the fflush function is called to forcibly fl the buffer.
Row buffering. In this case, when a new line character is encountered in the input and output, the standard I/O Library performs the I/O operation, which allows us to output one character at a time (such as the fputc function ), however, the actual I/O operation is performed only after a row is written. When a stream involves a terminal (such as standard input and standard output), row buffering is typically used.
Without buffering. The standard I/O Library does not buffer characters. If you use a standard I/O function to write a few characters to a stream without buffering, it is equivalent to writing these characters to an open file using a write System Call. Standard error conditions stderr are usually not cached, so that the error information can be displayed as soon as possible.
2. Open the stream
FILE * fopen (const char * filename, const char * type)
If the operation succeeds, the file pointer is returned. If the operation fails, NULL is returned.
Note the impact of the parameter type on the read/write operations of the opened stream.
Type description
R is read-only. The file must exist.
W is created for writing only. If the file already exists, the file content is truncated to 0.
A is used to open the file by adding data at the end of the file. If the file does not exist, create a new one.
R + opens for read/write; the file must exist
W + is created for reading and writing. If the file already exists, the file content is truncated to 0.
A + opens the file for reading and writing at the end of the file. If the file does not exist, create a new one. You can use fseek or rewind to relocate the file pointer,
However, the write operation will move the file pointer back to the end of the file.
Use character B as part of type to distinguish between text files and binary files. However, some systems are not differentiated, such as UNIX.
When a file is opened in read/write mode, that is, "+" exists in type, operations such as fflush or fseek, fsetpos, and rewind must be performed between the output operation and the input operation.
3. Close the stream
Int fclose (FILE * fp)
Before closing the stream, rinse the output data in the buffer and discard any input data in the buffer.
When the process ends normally, all the streams with unwritten buffer data are flushed and all the streams are closed. When an exception is terminated, the flushing buffer is not guaranteed.
4. Binary IO
Size_t fread (void * buf, size_t size, size_t count, FILE * fp );
Size_t fwrite (const void * buf, size_t size, size_t count, FILE * fp );
Returns the number of read/write objects.
Restriction: it can only be used to read data written on the same system.
References:
APUE
Author: iamzhaiwei