[Linux] Standard library IO interface, file operations: fopen, fwrite, fread, fclose

Standard library IO interface

fopen()

FILE * fopen ( const char * path, const char * mode );

Function : Open the file with the name specified in the parameter path and associate it with a stream, which can be identified by the returned file stream pointer in subsequent operations .
path: file name with path (if there is no path, the file will be created in the current directory)
mode: file opening method
Return value; success, return operation handle—file stream pointer; failure, return NULL

File open method

String that controls read and write permissions (must be specified)
Open method Description
“r” Open the file in "read-only" mode. Only reading is allowed, writing is not allowed. The file must exist, otherwise the opening fails.
“w” Open the file in "write" mode. If the file does not exist, then create a new file; if the file exists, then clear the contents of the file (equivalent to deleting the original file and creating a new file).
“a” Open the file in "Append" mode. If the file does not exist, create a new file; if the file exists, append the written data to the end of the file (the original content of the file is retained).
“r+” Open the file in "read and write" mode. Both can read and write, that is, update the file at will. The file must exist, otherwise the opening fails.
“w+” Open the file in the "write/update" mode, which is equivalent to the superimposed effect of "w" and "r". Both can read and write, that is, update the file at will. If the file does not exist, then create a new file; if the file exists, then clear the contents of the file (equivalent to deleting the original file and creating a new file).
“a+” Open the file in the "append/update" mode, which is equivalent to the superimposition of "a" and "r". Both can read and write, that is, update the file at will. If the file does not exist, create a new file; if the file exists, append the written data to the end of the file (the original content of the file is retained).
String that controls the way of reading and writing (can not be written)
“t” Text file. If not written, the default is "t".
“b” binary file.

On the whole, the file opening method is composed of six characters r, w, a, t, b, +, and the meaning of each character is:
r (read): read
w (write): write
a (append): append
t (text): text file
b(binary): binary file
+: read and write

fwrite()

size_t fwrite ( const void * ptr, size_t size, size_t nmemb, FILE * stream );

Function : Write an array of nmemb elements from the memory block pointed to by ptr to the current position in the stream, and the size of each element is size bytes. (The actual written size is size*nmemb )
ptr: data to be written,
size: block size; nmemb: number of blocks
Return value: success, return the number of blocks completely written to the file; failure, return 0

fread()

size_t fread ( void * ptr, size_t size, size_t nmemb, FILE * stream );

Function : Read an array of count elements from the stream, each element is size bytes in size, and store them in the memory block specified by ptr.
ptr:
Pass in the first address of the buffer to store the read data size: block size; nmemb: number of blocks-size*nmemb is the total length to be read
stream: operation handle returned by fopen-file stream pointer
Return value:

If successful, it returns the number of blocks completely read; if it
fails, it returns 0; the
file reads to the end also returns 0; (you can use int feof (FILE* stream) to determine whether to read to the end of the file)
Recommendation: Set the block size 1. The number of blocks is set to length

fseek()

int fseek ( FILE * stream, long offset, int whence);

Function : the reading and writing position of the jump file
offset: the offset
whyce: the relative starting position of the offset

SEEK_SET: the start position of the file
;
SEEK_CUR: the current position; SEEK_END: ​​the end position of the file.

flclose()

int fclose ( FILE * stream );

Function: Close the file associated with the stream and disassociate it.

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/115297257