File stream
File streams can be written as text streams. Data is written as characters and these characters are organized as data rows. Each row ends with a line break, data such as int or double must be converted to characters before being written into the file. The second is binary stream. binary stream is the actual value written into the file. For example, values of the double type occupy 8 bytes in the file. Whether it is a text stream or a binary stream, it is a series of components in the file. The key lies in how we interpret the data, such as the character 'a ', the file stores the ASCII code 97. when reading this file, we can use it as the character 'a' or as an integer 97, so let's take a look at how to explain the data.
1. Text Stream
1. fopen
Use the fopen function to open the file. The function prototype is defined in stdio. h. The function prototype is as follows:
FILE * fopen (const char * path, const char * mode );
The mode indicates the file opening mode. The opening mode is as follows:
R read-only
R + readable and writable
W: Write only. If the file does not exist, it is created. If yes, the original data of the file is cleared.
W + is readable and writable. If the file does not exist, it will be created. Different from r +, opening the file will clear the original data of the file.
A. Open a write-only file in append mode. If the file does not exist, it is created. By default, the position Pointer Points to the end of the file.
A + opens the Read/Write File in append mode.
If the operation succeeds, the FILE pointer pointing to the FILE is returned; otherwise, NULL is returned.
Note: When you open a file in readable and writable mode, you cannot read the data immediately after the data is written, because the position of the file read/write pointer has changed and the current read/write location needs to be changed, call the rewind function. The function prototype is as follows:
Void rewind (FILE * stream );
The rewind function sets the file read/write pointer to the file header. You can also use the fseek function. The rewind function is equivalent to fseek (fp, 0, SEEK_SET ).
Another reason is that the written data is not immediately written to the file, but stored in the buffer. When switched to read mode, the data may not be written to the file, resulting in data loss, you can use the fflush function to write data in the buffer to a file.
Similar to switching from the write mode to the Read mode, when switching from the Read mode to the write mode, the current read/write location of the file may be different from what you think, resulting in overwriting the original data. You also need to change the read/write location first.
2. fclose
The fclose function is used to close files. The function prototype is as follows:
Int fclose (FILE * fp );
0 is returned. Otherwise, EOF is returned.
EOF is a special character, called the end character of a file, which is defined in stdio. h.
3. rename
Rename is a file rename function. The function prototype is as follows:
Int rename (const char * oldpath, const char * newpath );
Oldpath is the original file name and newpath is the new file name. If the operation is successful, 0 is returned. Otherwise, a non-0 value is returned. when calling the rename function, the file must be closed; otherwise, the Operation will fail.
4. remove
The remove function is used to delete files. The function prototype is as follows:
Int remove (const char * pathname );
When you call the remove function, the file must be closed. Otherwise, the operation fails.
5. fputc
The fputc function writes a character to a file. If the operation succeeds, the written character is returned. Otherwise, the EOF function is returned. The function prototype is as follows:
Int fputc (int c, FILE * stream );
In fact, characters are not written to physical files one by one, which is extremely inefficient. Write characters are stored in a buffer in the memory. after a certain number of characters are accumulated in the buffer, they are written into the file at a time.
Putc is equivalent to fputc, and their parameters and return values are the same, but putc is implemented as a macro in the standard library, and fputc is a function.
6. fgetc
Fgetc is opposite to fputc. fgetc reads a character from the file. If the operation succeeds, the read character is returned. Otherwise, the EOF is returned. The function prototype is as follows:
Int fgetc (FILE * stream );
Fgetc does not operate on physical files every time. When reading a character, it reads a whole piece of data to the buffer. Then, fgets only reads a character from the buffer until the buffer is empty, read a block of data into the buffer.
Like putc, getc is equivalent to fgetc.
5. fputs
Similar to puts, fputs writes strings to text files, but puts writes strings to stdout. The function prototype is as follows:
Int fputs (const char * s, FILE * stream );
If the operation is successful, 0 is returned; otherwise, EOF is returned.
Note: fputs is used to write strings into the file until '\ 0' is encountered, but' \ 0' is not written to the file.
6. fgets
The fgets function reads strings from text files. The function prototype is as follows:
Char * fgets (char * s, int size, FILE * stream );
Note: fgets reads the string from the file until it reads the '\ n' character or the size-1 character. If it reads the' \ n' character, it retains the '\ n' character in the string and appends the' \ 0' character at the end of the string. If the operation succeeds, the read string pointer is returned, otherwise, NULL is returned.
7. fprintf
Like printf, the printf operation object is stdout, and fprintf receives a FILE pointer parameter. The function prototype is as follows:
Int fprintf (FILE * stream, const char * format ,...);
8. fscanf
Similar to scanf, fscanf operates on stdin and fscanf receives a FILE pointer parameter. The function prototype is as follows:
Int fscanf (FILE * stream, const char * format ,...);
If an error is returned, EOF is returned, and the number of read values is returned.
9. fflush
The fflush function writes data in the buffer to a file. The function prototype is as follows:
Int fflush (FILE * stream );
0 is returned for success, and EOF is returned for failure.
10. handle errors
Generally, error messages are written to stderr instead of stdout, because stdout can be redirected, for example:
FILE * fp = NULL;
If (fp = fopen ("test.txt", "r") = NULL ){
Fprintf (stderr, "Can't open file !!! \ N ");
Exit (1 );
}
Ii. binary stream
To read and write files in binary mode, you only need to add B in file opening mode, such as rb, wb, and rb +.
1. fwrite
The function prototype is as follows:
Size_t fwrite (const void * ptr, size_t size, size_t nmemb,
FILE * stream );
The fwrite function does not check whether the file is opened in the text or binary mode. Of course, you can also use fwrite to write data to the text file, but the result is hard to understand. The fwrite instance is as follows:
FILE * fp = NULL;
Int data = 10;
Fp = fopen ("test", wb );
If (fp! = NULL ){
Fwrite (& data, sizeof (int), 1, fp );
}
The parameter size in the fwrite function is the number of bytes for a single element, and nmemb is the number of elements. Therefore, the total number of bytes written is size * nmemb.
2. fread
The function prototype is as follows:
Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream );
3. Random File Access
In many cases, you need to randomly access the data in the file instead of sequential access, which involves modifying the current read/write location of the file. Note that the write operation location cannot be modified in append mode, because the data is always written to the end of the file and the location of the read operation can be modified.
(1) ftell
The ftell function returns the current read/write operation location. The function prototype is as follows:
Long ftell (FILE * stream );
(2) fgetpos
Fgetpos is the same as the ftell function and returns the current read/write operation location. The function prototype is as follows:
Int fgetpos (FILE * stream, fpos_t * pos );
Unlike the ftell function, the read/write position is stored in the pos parameter. If the input parameter is successful, 0 is returned. If the input parameter fails, a non-0 value is returned. For example:
Fpos_t here = 0;
Fgetpos (fp, & here );
(3) fsetpos
The fsetpos function corresponds to the fgetpos function. The function prototype is as follows:
Int fsetpos (FILE * stream, fpos_t * pos );
Fsetpos can only be used with fgetpos, that is, only the value obtained by fgetpos can be used as the pos parameter, for example, fsetpos (fp, & here );
Like fgetpos, 0 is returned for success, and non-0 is returned for failure.
(4) fseek
The same ftell function corresponds to fseek. The function prototype is as follows:
Int fseek (FILE * stream, long offset, int whence );
The first parameter is the file pointer, the second parameter is the offset relative to the third parameter, and the third parameter is the starting position. The values include SEEK_SET, SEEK_CUR, and SEEK_END, which respectively indicate the Beginning of the file, the current location and the end of the file.