Three streams are predefined for a process and can be automatically used by the process. They are standard input, standard output, and standard error.
The standard I/O Library provides buffer to minimize the number of read and write operations.
The standard I/O Library provides three types of buffering:
1. Full Buffer: Perform actual I/O operations after the standard I/O buffer is filled. Files residing on the disk are usually fully buffered by the standard I/O library. In terms of the standard I/O library, flush means that the content in the buffer zone is written to the disk. In terms of terminal drivers, flush indicates that data already stored in the buffer is discarded.
2. Row buffering: when the input or output encounters a line break, the standard I/O Library performs the I/O operation.
3. No buffering: The standard I/O Library does not store character buffering.
Standard Error stderr is usually not buffered, so that the error information can be displayed as soon as possible.
I. Function fopen
Function: open a file
Function prototype: file * fopen (
Const Char
* Path,
Const
Char
* Mode );
Returned value: After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
Using character B as a part of type makes it possible for the standard I/O system to distinguish between text files and binary files. Because the Unix kernel does not distinguish these two types of files, specifying character B as part of the type in the Unix environment does not actually work. Unless the stream references the terminal device, the stream is fully buffered when the stream is turned on by default. If a stream references a terminal device, the stream is buffered.
Ii. fclose Function
Function: disables an open stream.
Function prototype: int fclose (File * FP );
Rinse the output data in the buffer before the file is closed. Discard any input data in the buffer.
When a process ends normally (the exit function is called directly or returned from the main function), all standard I/O streams with unwritten buffer data will be flushed, all open standard I/O streams are automatically disabled.
3. Read and Write streams
Once a stream is opened, you can select among three different types of unformatted I/O to read, write, and operate on it:
(1) I/O of each character. One read or write character at a time. If the stream is buffered, the standard I/O function processes all the buffers.
(2) I/O of each row. If you want to read or write a row at a time, use fgets and fputs. Each line is terminated with a linefeed.
(3) Direct I/O. Fread and fwrite functions support this type of I/O.
1. Input Function (read one character at a time)
The following three functions can be used to read one character at a time.
IntGETC (File * FP );
IntFgetc (File
* FP );
IntGetchar (
Void); Return Value: if it succeeds, the next character is returned. If it has reached the end of the file or an error occurs, the EOF is returned. The getchar function is equivalent to GETC (stdin ).
When these three functions return the next character, they will convert their unsigned char type to int type. The constant EOF in <stdio. h> is required to be a negative number, which is usually-1.
Note that the three functions return the same value (EOF) whether an error occurs or the end of the file ). To distinguish between the two cases, you must call ferror or feof.
IntFerror (File * FP );
IntFeof (File * FP); two function return values: if the condition is true, a non-0 value is returned; otherwise, 0 is returned.
Void clearerr (File * FP );
In most implementations, two flags are maintained for each stream in the file object:
(1) Error mark
(2) End mark
Call the clearerr function to clear the two flags.
2. output function (output one character at a time)
IntPutc (
IntC, file * FP );
IntFpuc (
IntC, file * FP );
IntPutchar (
IntC );
Putchar (c) is equivalent to putc (C, stdout)
3. I/O for each row
Char
* Fgets (
Char* Buf,
IntN, file * FP );
Char
* Gets (
Char* BUF );
For fgets, you must specify the length of the buffer n. This function keeps reading the next line break, but cannot exceed n-1 characters. The characters read are sent to the buffer zone. The buffer ends with null characters.
IntFputs (
Char* STR, file * FP );
IntPuts (
Char* Str );
4. Binary I/O
Size_t fread (Void* Buffer, size_t size, size_t count, file * stream); function: read data from a file stream, read up to count elements, each element size byte, if the call is successful, the number of actually read elements is returned. If the call is unsuccessful, 0 is returned. Parameter: the memory address used by the buffer to receive data. The size must be at least size * count bytes. size: the size of a single element. The unit is the number of Count elements. Each element is a size byte. stream input stream return value: Number of actually read elements. if the return value is different from the Count value, the end of the file or an error may occur. get error information from ferror and feof or check whether it is reaching the end of the file.Size_t fwrite (Const Void* Buffer, size_t size, size_t count, file * stream); Note: This function operates files in binary format, not limited to the return values of text files: return the number of data blocks actually written. (1) buffer: A pointer. For fwrite, It is the address of the data to be output. (2) size: the number of single-word segments to be written; (3) count: number of data items to be written in size bytes; (4) stream: Destination File pointer; (5) return the actual number of data items written in count.
Iv. Stream locating
Function prototype:
LongFtell (File * FP );
The ftell () function is used to obtain the number of offset bytes from the current position of the file position pointer to the first part of the file.
Function prototype:IntFseek (File
* Stream,LongOffset,
IntWhere );
Function: relocate the internal position pointer of a file on a stream (data stream/file ).
Note: The file pointer does not point to the file/stream. The position Pointer Points to the byte location inside the file. As the file reads, the file pointer will not change to another file if it is not re-assigned.
The function sets the position of the file pointer stream. If the execution is successful, stream points to where (starting position of the Offset: File Header 0 (seek_set), current position 1 (seek_cur), and end 2 (seek_end) as the benchmark, offset offset (pointer offset. If the execution fails (for example, the offset value exceeds the object size), the position pointed to by stream is not changed.
Function prototype:
VoidRewind (File
* Stream );
Function: points the position pointer inside the file to the beginning of a stream (data stream/file ).
Note: It is not a file pointer but a position pointer inside the file. As the position pointer (pointing to the current read/write bytes) of the file is moved backward. The file Pointer Points to the entire file. If the file pointer is not re-assigned, it will not change.
The rewind function is equivalent (Void) Fseek (stream, 0l, seek_set );
Three streams are predefined for a process and can be automatically used by the process. They are standard input, standard output, and standard error.
The standard I/O Library provides buffer to minimize the number of read and write operations.
The standard I/O Library provides three types of buffering:
1. Full Buffer: Perform actual I/O operations after the standard I/O buffer is filled. Files residing on the disk are usually fully buffered by the standard I/O library. In terms of the standard I/O library, flush means that the content in the buffer zone is written to the disk. In terms of terminal drivers, flush indicates that data already stored in the buffer is discarded.
2. Row buffering: when the input or output encounters a line break, the standard I/O Library performs the I/O operation.
3. No buffering: The standard I/O Library does not store character buffering.
Standard Error stderr is usually not buffered, so that the error information can be displayed as soon as possible.
I. Function fopen
Function: open a file
Function prototype: file * fopen (
Const Char
* Path,
Const
Char
* Mode );
Returned value: After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
Using character B as a part of type makes it possible for the standard I/O system to distinguish between text files and binary files. Because the Unix kernel does not distinguish these two types of files, specifying character B as part of the type in the Unix environment does not actually work. Unless the stream references the terminal device, the stream is fully buffered when the stream is turned on by default. If a stream references a terminal device, the stream is buffered.
Ii. fclose Function
Function: disables an open stream.
Function prototype: int fclose (File * FP );
Rinse the output data in the buffer before the file is closed. Discard any input data in the buffer.
When a process ends normally (the exit function is called directly or returned from the main function), all standard I/O streams with unwritten buffer data will be flushed, all open standard I/O streams are automatically disabled.
3. Read and Write streams
Once a stream is opened, you can select among three different types of unformatted I/O to read, write, and operate on it:
(1) I/O of each character. One read or write character at a time. If the stream is buffered, the standard I/O function processes all the buffers.
(2) I/O of each row. If you want to read or write a row at a time, use fgets and fputs. Each line is terminated with a linefeed.
(3) Direct I/O. Fread and fwrite functions support this type of I/O.
1. Input Function (read one character at a time)
The following three functions can be used to read one character at a time.
IntGETC (File * FP );
IntFgetc (File
* FP );
IntGetchar (
Void); Return Value: if it succeeds, the next character is returned. If it has reached the end of the file or an error occurs, the EOF is returned. The getchar function is equivalent to GETC (stdin ).
When these three functions return the next character, they will convert their unsigned char type to int type. The constant EOF in <stdio. h> is required to be a negative number, which is usually-1.
Note that the three functions return the same value (EOF) whether an error occurs or the end of the file ). To distinguish between the two cases, you must call ferror or feof.
IntFerror (File * FP );
IntFeof (File * FP); two function return values: if the condition is true, a non-0 value is returned; otherwise, 0 is returned.
Void clearerr (File * FP );
In most implementations, two flags are maintained for each stream in the file object:
(1) Error mark
(2) End mark
Call the clearerr function to clear the two flags.
2. output function (output one character at a time)
IntPutc (
IntC, file * FP );
IntFpuc (
IntC, file * FP );
IntPutchar (
IntC );
Putchar (c) is equivalent to putc (C, stdout)
3. I/O for each row
Char
* Fgets (
Char* Buf,
IntN, file * FP );
Char
* Gets (
Char* BUF );
For fgets, you must specify the length of the buffer n. This function keeps reading the next line break, but cannot exceed n-1 characters. The characters read are sent to the buffer zone. The buffer ends with null characters.
IntFputs (
Char* STR, file * FP );
IntPuts (
Char* Str );
4. Binary I/O
Size_t fread (Void* Buffer, size_t size, size_t count, file * stream); function: read data from a file stream, read up to count elements, each element size byte, if the call is successful, the number of actually read elements is returned. If the call is unsuccessful, 0 is returned. Parameter: the memory address used by the buffer to receive data. The size must be at least size * count bytes. size: the size of a single element. The unit is the number of Count elements. Each element is a size byte. stream input stream return value: Number of actually read elements. if the return value is different from the Count value, the end of the file or an error may occur. get error information from ferror and feof or check whether it is reaching the end of the file.Size_t fwrite (Const Void* Buffer, size_t size, size_t count, file * stream); Note: This function operates files in binary format, not limited to the return values of text files: return the number of data blocks actually written. (1) buffer: A pointer. For fwrite, It is the address of the data to be output. (2) size: the number of single-word segments to be written; (3) count: number of data items to be written in size bytes; (4) stream: Destination File pointer; (5) return the actual number of data items written in count.
Iv. Stream locating
Function prototype:
LongFtell (File * FP );
The ftell () function is used to obtain the number of offset bytes from the current position of the file position pointer to the first part of the file.
Function prototype:IntFseek (File
* Stream,LongOffset,
IntWhere );
Function: relocate the internal position pointer of a file on a stream (data stream/file ).
Note: The file pointer does not point to the file/stream. The position Pointer Points to the byte location inside the file. As the file reads, the file pointer will not change to another file if it is not re-assigned.
The function sets the position of the file pointer stream. If the execution is successful, stream points to where (starting position of the Offset: File Header 0 (seek_set), current position 1 (seek_cur), and end 2 (seek_end) as the benchmark, offset offset (pointer offset. If the execution fails (for example, the offset value exceeds the object size), the position pointed to by stream is not changed.
Function prototype:
VoidRewind (File
* Stream );
Function: points the position pointer inside the file to the beginning of a stream (data stream/file ).
Note: It is not a file pointer but a position pointer inside the file. As the position pointer (pointing to the current read/write bytes) of the file is moved backward. The file Pointer Points to the entire file. If the file pointer is not re-assigned, it will not change.
The rewind function is equivalent (Void) Fseek (stream, 0l, seek_set );