Last updated on May. 9, 2024, created on May. 6, 2024.
数据文件分为两类:文本文件(ASCII)、二进制文件(Binary)
文件变量的类型为:FILE,利用它可以创建文件指针变量:FILE *f;
文件打开函数:f=fopen("C:\\mytext.txt","r");
Method |
Type |
Permission |
Existence |
Return Value |
Annotation |
|---|---|---|---|---|---|
| r | File | Read Only | Necessary | Successful: File* (head of the file) Failed: NULL |
|
| r+ | File | Read and Write | Necessary | Successful: File* (head of the file) Failed: NULL |
|
| rb+ | Binary | Read and Write | Necessary | Successful: File* (head of the file) Failed: NULL |
|
| rt+ | ASCII | Read and Write | Necessary | Successful: File* (head of the file) Failed: NULL |
|
| w | File | Write Only | Not Necessary | Successful: File* (head of the file) Failed: NULL |
(1) If the file to be open already exists, the contents will be overwritten; if not, computer will create a new file. (2) If the file to be open is not editable, the function will return NULL. |
| w+ | File | Read and Write | Not Necessary | Successful: File* (head of the file) Failed: NULL |
(1) If the file to be open already exists, the contents will be overwritten; if not, computer will create a new file. (2) If the file to be open is not editable, the function will return NULL. |
| a | File | Write Only(Add Only) | Not Necessary | Successful: File* (head of the file) Failed: NULL |
(1) If the file to be open already exists, the contents will be overwritten; if not, computer will create a new file. (2) If the file to be open is not editable, the function will return NULL. |
| a+ | File | Read and Write(Add Only) | Not Necessary | Successful: File* (head of the file) Failed: NULL |
(1) If the file to be open already exists, the contents will be overwritten; if not, computer will create a new file. (2) If the file to be open is not editable, the function will return NULL. (3) Read from the head of the file and add from the end. |
文件关闭函数:fclose(f);
| Method | Description | Return Value |
|---|---|---|
| ch1=fgetc(f); | Read a character | Successful: one character Failed: EOF(-1) |
| fputc('a',f); | Write a character | Successful: the character you write Failed: EOF(-1) |
| fgets(ch,n,f); | Read a string with a length of n-1, the last character is '\0', and store it in ch. Note that '\n' will also be read if it is before the end of reading process. | Successful: address of ch Failed: NULL |
| fputs(ch,f); | Write a string | Successful: 0 Failed: non-zero value |
| fscanf(f,format,ch); | Formatted read a string | Successful: count Failed: EOF(-1) |
| fprintf(f,format,ch); | Formatted write a string | Successful: count Failed: EOF(-1) |
| fread(ch,size,count,f); | Read a data block | Successful: (actual/successful-read-data)count Failed: 0 |
| fwrite(ch,size,count,f); | Write a data block | Successful: (actual/successful-read-data)count Failed: 0 |
| Method | Annotation |
|---|---|
| fseek(f,(long int)offset,origin); | offset:10L(10 bytes after origin),-30L(30 bytes before origin) origin:0(begin),1(current),2(end) |
| ftell(f); | This method can be adopted to calculate the size of file. |
| rewind(f); | Move the pointer to the beginning of the file. |
| feof(f); | Judge if pointer points to the end of the file. |
| rename(old_filename,new_filename); | Rename a file. |
| remove(filename); | Remove a file. |