File operations
Delete the file
int remove(const char *filename);
Copy the code
The remove function is similar to the Unix/Linux command rm, which takes a file name and then deletes the corresponding file. For most file systems, deleting a file simply deletes the file index, not erasing the contents of the hard disk, so it can be done in a flash.
The function returns 0 on success and non-0 on failure.
Rename file
int rename(const char *old, const char *new);
Copy the code
The rename function is similar to the Unix/Linux mv command. It not only renames a file, but also moves it to another directory. The rename function fails if old and new correspond to files on different file systems. Because moving files across file systems requires copying files, it works differently from moving files within the same file system.
The function returns 0 on success and non-0 on failure.
Create temporary files
FILE *tmpfile(void);
Copy the code
The tmpFile function creates and opens a temporary file with the same name, which is automatically deleted when the file is closed or the program exits. The file is opened wb+, and for Unix/Linux, the temporary file is located in the/TMP directory.
The function returns a FILE * pointer (that is, a stream) to the temporary FILE on success, or a null pointer on failure.
Open the file
FILE *fopen(const char * restrict filename,
const char * restrict mode);
Copy the code
The fopen function opens a file and associates it with a newly created stream. The function returns the stream on success, and a null pointer on failure.
Mode is how the file is opened. It is a string, and the function parses each character in turn.
The basic opening methods are r, W, and A, which are mutually exclusive and must be placed at the beginning of the mode string.
- R: Reads files. If the file does not exist or has no access permission, it fails to open. Files are not truncated.
- W: Writes files. The file is created if it does not exist and truncated if it does (unless x is specified).
- A: Append to the file. The file is created if it does not exist, and appended to the end if it does exist.
In addition, there are some additional pattern qualifiers that cannot appear independently and must rely on the basic opening method mentioned above. They are not mutually exclusive and can be combined on demand.
- B: Open in binary mode. Without b, the default is text mode.
- + : Opens in update mode. You can read and write.
- X: opens in exclusive mode and can only be used with W. If the file does not exist, the file is created. If the file exists, the file fails to open.
When a file is opened in update mode (+), the stream can be read or write, but can only be in either state at a time. File location indicators and buffers are common between states. Read operation can set the flow state to read, write operation can set the flow state to write. In many implementations, switching to the read state flushs the buffer in the write state.
If you open a file in A or AB mode, it prevents functions such as fseek from modifying the file location indicator, which always points to the end of the file, and all output is forced to append to the end of the file.
If the file is opened in ** A + or AB +** mode, functions such as fseek can take effect to adjust the file location when it is read. But once written, the file location indicator is set to the end of the file, and all output is forced to append to the end of the file.
Close the file
int fclose(FILE *stream);
Copy the code
The fclose function closes an open file and releases its associated stream. Fclose flushs the buffer before closing the file.
This function returns 0 on success and EOF on failure.
Whether the file is successfully closed or not, the stream is unassociated with the file, freeing the stream and the automatically allocated buffer.
When the program ends normally (the main function returns or exit is called), all open streams are closed, with the same effect as when fclose is called.
Flow operation
Redirect the flow
FILE *freopen(const char * restrict filename,
const char * restrict mode,
FILE * restrict stream);
Copy the code
The freopen function closes and unassociates the original file, then opens a new file and associates it with the stream.
The function returns the stream on success, or a null pointer on failure.
The freopen function closes the original file first. Whether the original file is closed successfully does not affect subsequent operations and the return value. Attempts to open the specified file, or returns a null pointer if the file fails to open. If successfully opened and associated with a new file, the stream’s error flag and EOF flag are reset.
If filename is a null pointer, the freopen function tries to change the stream mode to the mode specified by mode, without closing or disassociating the original file.
Freopen is mainly used to redirect stdin, stdout, and stderr, but it is more concise to just open the file and assign to these Pointers.
Flush buffer
int fflush(FILE *stream);
Copy the code
The fflush function flusher the buffers for a given output stream, including streams that are open in write mode, open in append mode, or open in update mode and in write. The contents of the buffer are immediately written to the corresponding file.
This function returns 0 on success and EOF on failure.
The behavior of calling fflush on the input stream is undefined. The C standard does not provide a way to flush the input buffer.
If the stream is null, fflush flusher all output streams.
Setting the buffer
The buffer should be set after the stream is associated with a file and before any other operations are performed. Otherwise, the behavior is undefined.
When a stream is associated with a file, the buffer is not allocated until any other operation is done, and it is safe to set the buffer without causing content loss.
setvbuf
int setvbuf(FILE * restrict stream,
char * restrict buf,
int mode, size_t size);
Copy the code
The setvbuf function will take the memory area referred to by buf as the buffer for the stream, with size indicating the buffer size and mode indicating the buffer mode.
The function returns 0 on success and non-0 on failure.
If buf is a null pointer, the setvbuf function will automatically allocate a buffer to the stream. Size specifies the size of the buffer.
The mod accepts three predefined macros:
- _IOFBF: full buffer
- _IOLBF: row buffer
- _IONBF: no buffer
setbuf
void setbuf(FILE * restrict stream,
char * restrict buf);
Copy the code
The setbuf function is similar to the setvbuf function, but by default the macro _IOFBF is used as the buffer and the macro BUFSIZ is used as the buffer size. If buf is a null pointer, use the macro _IONBF as the buffer.
The setbuf function returns no value.
File location operation
For streams that support location requests (such as streams associated with normal files, rather than with terminals), include a file location indicator for file location operations.
In many implementations, setting the file location indicator will flush the output buffer.
ftell
long int ftell(FILE *stream);
Copy the code
The ftell function gets the value of the file location indicator, which is the offset from the beginning of the file. Starting at 0, in bytes.
The function returns an integer value representing the offset on success, and -1 on failure.
fseek
int fseek(FILE *stream, long int offset, int whence);
Copy the code
The fseek function is used to set the value of the file location indicator. whence indicates the starting point, and offset indicates the offset from the starting point.
The function returns 0 on success and non-0 on failure.
whence accepts three predefined macros:
- SEEK_SET: the start of the file with an offset of 0
- SEEK_CUR: Current location, the value of the current file location indicator
- SEEK_END: The end of the file, after the last byte
The value of offset can be positive, negative or 0.
If the fseek function executes successfully, the EOF flag of the stream is reset.
rewind
void rewind(FILE *stream);
Copy the code
The rewind function rewinds the file location indicator back to the beginning of the file. This function has no return value.
If the rewind function executes successfully, it resets the EOF flag of the stream as well as the error flag.
fgetpos
int fgetpos(FILE * restrict stream,
fpos_t * restrict pos);
Copy the code
The fgetpos function stores the value of the file location indicator and the parse state of the wide character stream into the object pointed to by pos.
The function returns 0 on success and non-0 on failure.
fsetpos
int fsetpos(FILE *stream, const fpos_t *pos);
Copy the code
The fsetpos function sets the value of the file location indicator and the parsing status of the wide character stream, depending on the object pos points to.
The function returns 0 on success and non-0 on failure.
The object pos refers to should be the result of the last call to fgetpos on the same stream. If the fsetpos function executes successfully, the EOF flag of the stream is reset.
Flow state operation
feof
int feof(FILE *stream);
Copy the code
The feOF function checks whether the EOF flag of the stream is set.
If the EOF flag of the stream is set, return non-zero; Otherwise 0 is returned.
ferror
int ferror(FILE *stream);
Copy the code
The ferror function checks whether the error flag for the stream is set.
If the stream’s error flag is set, return non-zero; Otherwise 0 is returned.
clearerr
void clearerr(FILE *stream);
Copy the code
The clearErr function resets the EOF flag and error flag of the stream. This function has no return value.
perror
void perror(const char *s);
Copy the code
The perror function maps the ordinal number of the error flag to an error message string and prints it to stderr. This function has no return value.
The error message string contains a newline character at the end. If s is a valid string (not a null pointer and not pointing to ‘\0’), the contents of the string are printed first, followed by a “: “(colon and space), and then the contents of the error message string.