Standard file operations in C language

Mainly introduce the api of C language file operation.

File pointers and file switches

FILE * p; file pointer, you can directly manipulate the contents of the file.
fopen(dir,type); open a file and return the file pointer, pointing to the first position in the file.
fclose(p); close the file
p = NULL; after use, pay attention to assign the file pointer to NULL, so that it will not become a wild pointer.

int main(){
    FILE * p;
    p = fopen("d://a.txt","r");
    if(p == NULL){
        printf("文件打开失败\n");
        exit(-1);
    }
    fclose(p);
    p = NULL;   
    return 0;
}

file open type

There are many types of file opening. The most commonly used ones are
r, which opens a text file in read-only mode, fails to open if it does not exist
w, opens a file in write-only mode, creates
a if it does not exist, and opens a file in append mode If the file does not exist, create
r+, read and write methods, and fail to open
w+ if it does not exist, open or create a new file in read-write mode, allowing read and write.
a+, open the text file in read and write mode, append data at the end of the text, create one if it does not exist.
ab, open a binary file by appending, if it does not exist, it will fail to open.
rb, open a binary file in read mode, if it does not exist, it will fail to open.
wb, open a binary file for writing, create it if it does not exist.
There are many above, but in fact, it is very easy to remember, you only need to know 3 words, read, write, append

Commonly used functions for manipulating files

fgetc(p) != EOF; read a character, the EOF here is actually the constant -1

int main(){
    FILE * p;
    if((p = fopen("d://a.txt","r")) == NULL){
        printf("文件打开失败\n");
    };
    char c[100];
    int i = 0;
    while((c[i] = fgetc(p)) != EOF){
        i++;
    }
    c[i] = '\0';
    printf("%s",c);
    fclose(p);
    p = NULL;
    return 0;
}

fputc(c,p); write out a character, c is the character, p is the file pointer.

char s[] = "qwe";
int length = strlen(s);
int index = 0;
while(index < length){
    fputc(s[index ++],p);//p为文件指针
}

ftell(p); Get the position of the pointer of the current file
fseek(p, 0L, SEEK_END); Set the position of the current file pointer, there are three parameters here, the first parameter is the file pointer, the second parameter is the cheap amount, the first The three parameters are the location of the setting.
Usually , we obtain the volume of the file through the file pointer

int curIndex = ftell(p);
fseek(p,0L,SEEK_END);
int lastIndex = ftell(p);
int length = lastIndex - curIndex;
feek(p,curIndex,SEET_SET); //将文件指针设置回原位置

fwrite(&node,sizeof(Node),1,p);
here are 4 parameters, the first parameter is which structure will be stored, the second parameter is the size of the structure, and the third parameter is how many structures to store body, p is the file pointer. Returns 1 if the write is successful.
fread(&node,sizeof(Node),1,p); The first parameter is the structure to be stored in, and the other parameters have the same meaning as fwrite. This function returns 1 on successful read.

typedef struct Student {
    int age;
    char * name;
    int c;
    int d;
}Student;

void writeTest() {
    Student s;
    s.age = 1;
    s.c = 1;
    s.d = 1;
    s.name = (char *)malloc(10);
    strcpy(s.name, "abcde");
    FILE * f;
    if ((f = fopen("D://a.dat", "wb")) == NULL) {
        printf("程序打开失败\n");
        exit(-1);
    }
    fwrite(&s, sizeof(s), 1, f);
    fwrite(s.name, 10, 1, f);
    fclose(f);
    f = NULL;   
    free(s.name);
}

void readTest() {
    Student s;
    FILE * p;
    if ((p = fopen("D://a.dat", "rb")) == NULL) {
        printf("程序打开失败\n");
        exit(-1);
    }
    fread(&s, sizeof(s), 1, p);
    s.name = (char *)malloc(10);
    fread(s.name, 10, 1, p);
    printf("%s", s.name);
    free(s.name);
    fclose(p);
    p = NULL;
}

fputs(str,f);//Two parameters, the first parameter is the pointer of the string to be stored, the second parameter is the file pointer, directly output all the strings in str, and no newline will be output after the output is completed symbol. Note the difference here with puts.

void fputsTest() {
    FILE * f;
    if ((f = fopen("d://a.txt", "w")) == NULL) {
        printf("文件打开失败\n");
        exit(1);
    }
    const char * str = "hello word";
    const char * str2 = "abc";
    fputs(str, f);
    fputs(str2, f);
    fclose(f);
    f =`
NULL;
}

fgets(str,length + 1,f); There are three parameters here, the first parameter is which string to store, and the second parameter is how many bytes to read. Note that it should be written as the number of bytes read + 1, the reason is that a '\0' is added at the end, and the third parameter is the file pointer. At the same time, it should be noted that Hanzi occupies 2 bytes and occupies 2 bytes.

void fgetsTest() {
    FILE * f;
    if ((f = fopen("d://a.txt","r")) == NULL) {
        puts("文件打开失败");
        exit(1);
    }
    char s[200];
    fgets(s, 5,f);
    puts(s);
    fclose(f);
    f = NULL;
}

Focus on mastering, fgetc, fputc, fread, fwrite; 4 functions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569031&siteId=291194637