1. What is a file and its unique identification: three elements (path, file name, suffix)
File: a collection of data stored on external media
2. File operation function
2.1 fopen return value: two parameters of FILE pointer FILE * (the first parameter [FILE name] and the second parameter [how to open the FILE])
2.2 there is only one Fclose} parameter (released file stream pointer) 1 Prevent memory leakage 2 Opening again without releasing may cause an error
2.3 fread ^ return value: returns the number of items actually read ^ four parameters (1. Where the data read from the file is stored in the memory, 2. The size of the read item, 3. The maximum number of items read, and 4. From which file)
If the returned value of fread is smaller than the size of the input buffer, it must be read; otherwise, it cannot be guaranteed to be read
2.4 fwrite {return value: returns the number of items actually written} four parameters (1. Data to be written to the file, 2. Size of items to be written, 3. Number of items to be written, 4. Where is the file to be written)
2.5 fseek# adjust the position of the cursor in the file # SEEK_SET SEEK_CUR SEEK_END
2.6 ftell , tell me the position of the cursor in the file , the return value is an integer value , representing how many bytes the cursor is from the beginning of the file
3. Data stream (input stream and output stream) standard input stream device: keyboard; standard output stream device: screen
3.1 text data stream (text file. TXT)
3.2 binary data stream (binary file song video picture executable file)
3.3 input stream (write from file to memory) output stream (write from memory to file)
4. Document operation steps: 1 Open file 2 Execute file operation function 3 Close file
5. File pointer
6. Absolute and relative paths of files
7. Read and write binary files and text files respectively
8. Precautions: 1 Fopen function if the mode is "w", if there is data in it, it will be cleared and opened again, resulting in data loss
(0)
int main() { const char *path = "1.txt";//Relative path const char *path2 = "D:\\1.txt";//Absolute path //const char *path3 = "D: .txt";//error \ Escape 2 is not a correct file unique ID const char *path4 = "D:/3.txt"; //ok //printf("%s\n%s\n%s\n", path, path2, path4); FILE * fw = fopen(path4, "w");//Open the file to write //int a = 123456; // 40 E2 01 00 4 bytes //fwrite(&a, sizeof(int), 1, fw); const char *str = "123456"; // '1' 2 '3' 4 '5' 6 '6 bytes fwrite(str, sizeof(char), strlen(str), fw);//Write str to fw fclose(fw);//1. Prevent memory leakage 2 Opening again without releasing may cause an error return 0; }
(1) Read data from memory to a text file} and write it to memory (input stream)
#include <stdio.h> #include <string.h> int main() { const char *path = "xyz.txt"; FILE *fw = fopen(path, "w");//Open the file in write form and receive it with * fw const char *str = "hello world!";//Define a string * str fwrite(str, sizeof(char), strlen(str), fw);//Write str to file fw fclose(fw); return 0; }
(2) Read data from a text file} and write it to memory (input stream)
#include <stdio.h> #include <string.h>int main() { const char *path = "xyz.txt"; FILE *fr = fopen(path, "r");//Open file as read //You need to apply for a piece of memory to store the data read step by step from the file //char buff[100] = {0};//char buff[100] = ""; //int size = 0; //while((size = fread(buff, sizeof(char), 100, fr)) == 100) //{ // printf("%s", buff); //} //buff[size] = '\0'; //printf("%s\n", buff); //The following methods are used more char cr; while(fread(&cr, sizeof(char), 1, fr) > 0)//Read success: 1 read failure: 0 { printf("%c", cr); } printf("\n"); fclose(fr); return 0; }
(3) Write data from memory to binary (input stream)
int main() { FILE *fwb = fopen("abc.txt", "wb");//b: Binary file int arr[] = {12,23,34,45,56,67,78,89,90,100}; fwrite(arr, sizeof(int), sizeof(arr)/sizeof(arr[0]), fwb); fclose(fwb); return 0; }
(4) Read data from binary file and write it to memory (input stream)
int main() { FILE * frb = fopen("abc.txt", "rb"); int tmp;//Similar to the buff above while(fread(&tmp, sizeof(int), 1, frb) > 0) { printf("%d ", tmp); } printf("\n"); fclose(frb); return 0; }
(5) Function to adjust cursor position in file
int main() { const char *path = "www.txt"; FILE *fw = fopen(path, "w"); const char *str1 = "hello world"; const char *str2 = "wwwww yyyyy"; fwrite(str1, sizeof(char), strlen(str1), fw); //fseek(fw, 0, SEEK_SET);// SEEK_SET start position, cursor offset 0 bytes from start position //fseek(fw, -5, SEEK_END);//SEEK_END end position, cursor offset end position - 5 bytes fseek(fw, 8, SEEK_SET);//Offset start position 8 bytes fwrite(str2, sizeof(char), strlen(str2), fw);//Offset the start position by 8 bytes, and the result is hello wowwww yyyyyy long flg = ftell(fw);//Return cursor position printf("%d\n", flg);//Print cursor position fseek(fw, -10, SEEK_CUR);//Offset current position - 8 bytes flg = ftell(fw);//Return cursor position printf("%d\n", flg);//Print cursor position fclose(fw); return 0; }