Stream opening and closing

If you need to read or write a file, you should first get a stream pointer, or you should open the file first.
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fd, const char *mode);
FILE *freopen(const char *path, const char mode, FILE stream );
The functions for opening files using standard I/O are fopen(), fdopen(), freopen(). They can open files in different modes, and all return a pointer to FILE. The FILE structure is associated with the path (the FILE structure describes the path). Thereafter, the reading and writing of the file is carried out through this FILE pointer. Among them, fopen() can specify the path and mode of the open file, fdopen() can specify the file descriptor and mode to be opened, and freopen() can specify a specific I/O stream in addition to the opened file and mode. .
Among them, mode is used to specify the way to open the file. Note that this method represents the operation authority of the process (or executing program) on the file, not the execution authority of the user on the file. Table 2.1 shows the various values ​​of mode.
Table 2.1 Mode value description
mode function
r or rb to open the file in read-only mode, the file must exist
r+ or r+b to open the file in read-write mode, the file must exist
w or wb to open the file in write-only mode, if The file does not exist, it will be created automatically; if the file exists, the length of the intercepted file is 0, that is, the data in the file is cleared
w+ or w+b Open the file in read-write mode. If the file does not exist, it will be created automatically; if the file exists,
the length of the intercepted file is 0, that is, clear the data
a or ab in the file and open the file in write-only mode , If the file does not exist, it will be automatically created; if the file exists,
it will be appended to the end of the file, that is, the original data will not be cleared, and continue to write
a+ or a+b at the end of the data to open the file for reading and writing. If the file exists, it will be automatically created; if the file exists, it will be appended to the end of the file, that is, the original data will not be cleared, and continue to write or read at the end of the data.
Note that the b character is added to each option to tell the library to open The file is a binary file, not a plain text file. However, this symbol is ignored in Linux systems.
When the user program is running, the system will automatically open 3 stream pointers, they are: standard input stream pointer stdin, standard output stream pointer stdout and standard error output stream pointer stderr. These three stream pointers do not need to be declared and can be used directly by the process, as shown in Table 2.2.
Table 2.2 System Predefined Stream Pointer
Standard input 0 STDIN_FILENO stdin
standard output 1 STDOUT_FILENO stdout
standard error output 2 STDERR_FILENO stderr
stdin is used to read input from the standard input device (the default is the keyboard); stdout is used to send the input to the standard output device (the default Is the current terminal) output content; stderr is used to output error information to the standard error device (the current terminal by default). Because these three stream pointers are predefined by the system, they can be used directly, and they are often used to implement input and output on the terminal. Its essence is
the same as the pointer of fopen's return value FILE . One of them needs to be defined by the program, and the other is defined by the system. The associated objects are different. The objects associated with the three stream pointers predefined by the system can be considered as terminals, and the return value of fopen is FILE
The object associated with the pointer is the open file. Regarding its use scenario, it will be introduced in the following chapters.
The function to close the stream is fclose(). This function writes all the data in the buffer area of ​​the stream to the file and releases related resources. Sometimes functions can also be ignored, because all open stream pointers are automatically closed when the program ends.

#include <stdio.h>
 int fclose(FILE *fp);

Where fp is the opened stream pointer. The details are shown in Example 2-1.

  1 #include <stdio.h>
  2 
  3 int main(int argc, const char *argv[])
  4 {
  5     FILE *fp;
  6     if((fp = fopen("test.txt", "w")) == NULL){
  7         printf("fopen error\n");
  8         return -1;
  9     }
 10     fclose(fp);
 11     return 0;
 12 }    

After running, if the file does not exist, a test.txt file will be automatically created in the current working directory; if the file exists, the data in the test.txt file will be cleared. You can also change the mode parameter of the fopen() function to other tests.

Guess you like

Origin blog.csdn.net/anton_99/article/details/99230171