Close file
fclose(fp);
fp=NULL;
Open file
FILE* fp=fopen("xxx.xxx","xx");
The first parameter is the type of file set
The second parameter has the following
operation | effect | file does not exist |
---|---|---|
"w" (write only) | To output data, open a text file | Create a new file |
"r" (read only) | To output data, open an existing file | error |
"a" (added) | Add data to the end of a text file | error |
"rb" (read only) | To enter data, open a binary file | error |
"wb" (write only) | To output data, open a binary file | Create a new file |
"ab" (additional) | Add data to the end of a binary file | error |
"r +" (read / write) | To read and write, open a text file | error |
"w +" (read / write) | Create a new file for reading and writing | Create a new file |
"a +" (read / write) | Open a file and read and write at the end of the file | Create a new file |
"rb +" (read / write) | To read and write, open a binary file | error |
"wb +" (sketch) | To read and write, create a new text file | Create a new file |
"ab +" (read / write) | Open a binary file and read and write at the end of the file | Create a new file |
The functions related to sequential file reading and writing are
function | Function name | Apply to |
---|---|---|
Character input function | fgetc | All input streams |
Character output function | fputc | All output streams |
Text line input function | fgets | All input streams |
Text line output function | fputs | All output streams |
Format input function | fscanf | All input streams |
Format output function | fprintf | All output streams |
Binary input | fread | file |
Binary output | fwrite | file |
Keyboard -- standard input device ----- stdin
Screen -- standard output device ----- stdout
Use of fputc function
int main() { FILE* fpwrite = fopen("text.txt", "w"); if (fpwrite == NULL) { perror("Open failed"); return; } fputc('H', fpwrite); fputc('e', fpwrite); fputc('l', fpwrite); fputc('l', fpwrite); fputc('o', fpwrite); fclose(fpwrite); fpwrite = NULL; return 0; }
After running, you can see in the text.txt file
Use of fgetc function
text.txt contains Hello
int main() { FILE* fpread = fopen("text.txt", "r"); if (fpread == NULL) { perror("Open failed"); return; } printf("%c\n", fgetc(fpread)); printf("%c\n", fgetc(fpread)); printf("%c\n", fgetc(fpread)); printf("%c\n", fgetc(fpread)); printf("%c\n", fgetc(fpread)); fclose(fpread); fpread = NULL; return 0; }
After running, you can see it on the command console
Use of fputs function
int main() { FILE* fpwrites = fopen("text1.txt", "w"); if (fpwrites == NULL) { perror("Open failed"); return; } fputs("Hello", fpwrites); fputs("World", fpwrites); fclose(fpwrites); fpwrites = NULL; return 0; }
Display in text1.txt file
Use of fgets function
int main() { FILE* fpreads = fopen("text1.txt", "r"); char tmp[100] = { 0 }; if (fpreads == NULL) { perror("Open failed"); return; } //Output 50 characters from fpreads to tmp fgets(tmp, 50, fpreads); //Output tmp printf("%s\n", tmp); fclose(fpreads); fpreads = NULL; return 0; }
Display in command desk
The fprintf function uses
int main() { FILE* fpwrite = fopen("text2.txt", "w"); if (fpwrite == NULL) { perror("Open failed"); return; } fprintf(fpwrite, "%s %s %s %d", "Hello", "Word", "in", 2021); fclose(fpwrite); fpwrite = NULL; return 0; }
Display in text2.txt
Use of fscanf function
int main() { FILE* fpread = fopen("text2.txt", "r"); char arr1[20] = { 0 }; char arr2[20] = { 0 }; char arr3[20] = { 0 }; int year = 0; if (fpread == NULL) { perror("Open failed"); return; } //Store the data in fpread in Arr1, arr2, arr3 and year fscanf(fpread, "%s %s %s %d", arr1, arr2, arr3, &year); printf("%s %s %s %d", arr1, arr2, arr3, year); fclose(fpread); fpread = NULL; return 0; }
Display on command console
fwrit function uses
int main() { int a = 10000; FILE* fpwrite = fopen("text3.txt", "wb"); if (fpwrite == NULL) { perror("Open failed"); return; } //Store a element in four bytes in fpwrite fwrite(&a, 4, 1, fpwrite);//Write to file in binary mode fclose(fpwrite); fpwrite = NULL; return 0; }
Open text3.txt in a binary editor
Using the fread function
int main() { FILE* fpread = fopen("text3.txt", "rb"); int a = 0; if (fpread == NULL) { perror("Open failed"); return; } /* Read and display data */ fread(&a, sizeof(a), 1, fpread); printf("%d\n", a); fclose(fpread); fpread = NULL; return(0); }
Display on command console