c语言文件操作函数及实例

    xiaoxiao2021-03-25  112

    文件三步曲:

    打开读写关闭
    // // main.c // 文件操作 // Created by fzl // Copyright © All rights reserved. // #include <stdio.h> /** * fputc() */ void test1() { //1.打开文件 /* 参数1:打开文件的路径,绝对路径,相对路径 参数2:打开的方式, */ FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w"); if(fp==NULL) { perror("fopen failed"); } //2.对文件进行操作(读/写) char ch='a'; fputc(ch, fp); //3.关闭文件 fclose(fp); } /** * fgetc() */ void test2() { //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } //2. char ch=0; ch=fgetc(fp); printf("ch=%c\n",ch); // fclose(fp); } /** * fprintf() */ void test3() { int a=8; char ch='a'; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w"); if(fp==NULL) { perror("fopen failed"); } //2. fprintf(fp, "a=%d,ch=%c\n",a,ch); //3. fclose(fp); } /** *fscanf() */ void test4() { int a=0; char ch=0; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } //2. fscanf(fp, "a=%d,ch=%c\n",&a,&ch); printf("a=%d\nch=%c\n",a,ch); //3. fclose(fp); } /** * fputs() */ void test5() { char name[20]="rose"; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w"); if(fp==NULL) { perror("fopen failed"); } //2. fputs(name, fp); //3. fclose(fp); } /** * fgets() */ void test6() { char name[20]; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } //2. fgets(name, sizeof(name), fp); printf("name=%s",name); //3. fclose(fp); } /** fseek(),ftell() */ void test7() { char ch; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } fseek(fp, 0, SEEK_END); printf("文件大小=%ld\n",ftell(fp)); //2. ch=fgetc(fp); printf("ch=%c\n",ch); //3. fclose(fp); } void test8() { typedef struct Student { int age; char name[20]; }Student; Student stu={10,"rose"}; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w"); if(fp==NULL) { perror("fopen failed"); } //2. fwrite(&stu, sizeof(stu), 1, fp); //3. fclose(fp); } /** * fread() */ void test9() { typedef struct Student { int age; char name[20]; }Student; Student stu; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } //2. fread(&stu, sizeof(stu), 1, fp); printf("stu的name=%s,age=%d\n",stu.name,stu.age); //3. fclose(fp); } void test10() { typedef struct Student { int age; char name[20]; }Student; Student stu[3]={ {10,"rose"}, {20,"jack"}, {30,"white"} }; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "w"); if(fp==NULL) { perror("fopen failed"); } //2. //fwrite(stu, sizeof(stu), 1, fp); int i=0; for (; i<3; i++) { fwrite(&stu[i], sizeof(Student), 1, fp); } //3. fclose(fp); } void test11() { typedef struct Student { int age; char name[20]; }Student; Student stu[3]; //1. FILE *fp=fopen("/Users/fzl/Desktop/2.txt", "r"); if(fp==NULL) { perror("fopen failed"); } //2. //fread(stu, sizeof(Student), 3, fp); int i=0; for (; i<3; i++) { fread(&stu[i], sizeof(Student), 1, fp); printf("stu[%d].name=%s,age=%d\n",i,stu[i].name,stu[i].age); } //3. fclose(fp); } int main() { test11(); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-5416.html

    最新回复(0)