fopen
Function prototype
FILE *fopen(const char *path, const char *mode);
Parameters:
Path: the path of the file to be opened
Mode: file opening mode (r read, w write, + read and write, a add content, b binary)
r to open a file as read-only, the file must exist.
r + to open a file in read-write mode, the file must exist
w open the write only file. If the file exists, the file length will be cleared to 0, that is, the file content will disappear. If the file does not exist, the file is created. Bold style
w + open the read-write file. If the file exists, the file length will be cleared to zero, that is, the file content will disappear. If the file does not exist, the file is created. (common)
rb + read / write opens a binary file that allows data to be read.
a opens the write only file as an attachment. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
a + open read-write files in an additional manner. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
wb write only, open or create a new binary file; Only write data is allowed.
wb + read / write opens or creates a binary file that allows reading and writing.
ab + read / write opens a binary file, allowing you to read or append data at the end of the file.
FILE *fp;//Define a pointer to the FILE structure // Open a "ccc.txt" file in the current path with readable and writable. If it does not exist, create it fp=fopen("./ccc.txt","w+");
Return value
Success: it returns a pointer to the FILE structure that represents the newly created stream
(after the file is successfully opened, the file pointer to the stream will be returned)
Failure: it will return a null pointer and errno will indicate the nature of the problem
(if the file fails to open, NULL is returned and the error code is stored in errno)
fwrite
Function prototype
size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
ptr – is a pointer and, for fwrite, the address of the data to be output.
Size – this is the size of each element to be read, in bytes.
nmemb – this is the number of elements, and the size of each element is size bytes.
Stream – this is a pointer to a FILE object that specifies an input stream.
char *str="xixihaha!!!"; int write=fwrite(str,sizeof(char),strlen(str),fp); //int write=fwrite(str,sizeof(char)*strlen(str),1,fp); //This is OK, but the value of write is 1, and the value of write is related to the third parameter //If the third parameter is 100, the output write is 100 printf("write=%d \n",write);
write=11
Return value
If successful, the function returns a size_t object, representing the total number of elements, which is an integer data type. If this number is different from the nmemb parameter, an error is displayed.
fread
Function prototype
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
ptr – is a pointer. For fread, it is the storage address of the read data.
Size – this is the size of each element to be read, in bytes.
nmemb – this is the number of elements, and the size of each element is size bytes.
Stream – this is a pointer to a FILE object that specifies an input stream.
char *readBuf=NULL; char *str="xixihaha!!!"; readBuf=(char*)malloc(sizeof(char)*strlen(str)); //int read= fread(readBuf,sizeof(char)*strlen(str),1,fp); here If the third parameter is 1, then read The value of is also 1. If the third parameter is changed to 2, the result is output read Or 1 int read= fread(readBuf,sizeof(char),strlen(str),fp); //In read, if the third parameter is 100, the output result is 11 (read as many as you have) printf("read=%d \n",read); printf("%s\n",readBuf);
read=11
xixihaha!!!
Return value
The total number of elements successfully read is displayed in size_t nmemb object returns, size_ The t object is an integer data type. If the total number is different from the nmemb parameter, an error may have occurred or the end of the file may have been reached.
fclose
Prototype: int fclose(FILE*f);
Return value: for the output stream, the fclose function flushes the buffer before the file is closed,
If it executes successfully, fclose returns 0
fseek
Function prototype
int fseek(FILE *stream, long offset, int whence);
The first parameter: stream is the file pointer
The second parameter: offset is the offset, an integer represents the right offset, and a negative number represents the left offset
Third parameter:
SEEK_ Start of set file
SEEK_CUR current position
SEEK_END file end
Return value
0 is returned for success and - 1 is returned for failure
fgetc()fputc()feof()
EOF
This definition means that there is a hidden character "EOF" at the end of the document. When the program reads it, it will know that the file has reached the end. The while loop plus EOF (usually - 1) is usually used to judge the end of reading.
fgetc
ch=fgetc(fp);
Read a character from the specified file, that is, read a character from the file pointed to by fp and assign it to ch.
Return value
Success: the character obtained by the return value;
Failed: EOF(-1) returned.
fputc
fputc(ch,fp);
Write a character to the disk file, that is, output the character ch to the file pointed to by fp.
Return value
Success: the return value is the output character;
Failed: EOF(-1) returned.
feof
int main() { FILE *fp; int i; char c; fp= fopen("./text.txt","r"); while(1){ c=fgetc(fp); if(feod(fp)){ //Judge whether to read to the end of the file, if If it does not reach the file tail, it returns 0, and if it reaches the tail, it returns a non-zero integer. break; } printf("%c",c); } printf("\n"); fclose(fp); return 0; }