• C进阶-语言文件操作


    本章重点:

    什么是文件
    文件名
    文件类型
    文件缓冲区
    文件指针
    文件的打开和关闭文件的顺序读写文件的随机读写文件结束的判定

     1.  什么是文件

    磁盘上的文件是文件。
    但是在程序设计中,我们一般谈的文件有两种:程序文件、数据文件

     1.1 程序文件

    包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。

     1.2 数据文件

    文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。

     1.3 文件名

    一个文件要有一个唯一的文件标识,以便用户识别和引用。文件名包含3部分:文件路径+文件名主干+文件后缀
    例如: c:\code\test.txt
    为了方便起见,文件标识常被称为文件名。

     2. 文件的打开和关闭

     2.1 文件指针

    缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。

    每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE. 例如,VS2008编译环境提供的 stdio.h头文件中有以下的文件类型申明:

    1. struct _iobuf {
    2. char *_ptr;
    3. int _cnt;
    4. char *_base;
    5. int _flag;
    6. int _file;
    7. int _charbuf;
    8. int _bufsiz;
    9. char *_tmpfname;
    10. };
    11. typedef struct _iobuf FILE;

    不同的C编译器的FILE类型包含的内容不完全相同,但是大同小异。

    每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息,使用者不必关

    心细节。

    一般都是通过一个FILE的指针来维护这个FILE结构的变量,这样使用起来更加方便。

    下面我们可以创建一个FILE*的指针变量:

    FILE* pf;//文件指针变量

    定义pf是一个指向FILE类型数据的指针变量。可以使pf指向某个文件的文件信息区(是一个结构体变量)。通过该文 件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它关联的文件。 

    2.2 文件的打开与关闭

    1. int main()
    2. {
    3. //相对路径
    4. //绝对路径
    5. ///Users/fan/Documents/c_study/c_test26/data.txt
    6. //打开文件
    7. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    8. if (pf == NULL)
    9. {
    10. perror("fopen");
    11. return 1;
    12. }
    13. //读文件
    14. //关闭文件
    15. fclose(pf);
    16. pf = NULL;
    17. return 0;
    18. }

     3. 文件的顺序读写

     3.1 顺序读写函数介绍

     

     

    1. int main()
    2. {
    3. //相对路径
    4. //绝对路径
    5. ///Users/fan/Documents/c_study/c_test26/data.txt
    6. //打开文件
    7. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","w");
    8. if (pf == NULL)
    9. {
    10. perror("fopen");
    11. return 1;
    12. }
    13. //读文件
    14. //写文件
    15. // fputc('a',pf);
    16. // fputc('b',pf);
    17. // fputc('c',pf);
    18. int i = 0;
    19. for (i = 0; i < 26; i++)
    20. {
    21. fputc('a'+i,pf);
    22. }
    23. //关闭文件
    24. fclose(pf);
    25. pf = NULL;
    26. return 0;
    27. }
    1. int main()
    2. {
    3. //相对路径
    4. //绝对路径
    5. ///Users/fan/Documents/c_study/c_test26/data.txt
    6. //打开文件
    7. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    8. if (pf == NULL)
    9. {
    10. perror("fopen");
    11. return 1;
    12. }
    13. //读文件
    14. int ch = fgetc(pf);
    15. printf("%c\n",ch); //a
    16. ch = fgetc(pf);
    17. printf("%c\n",ch); //b
    18. ch = fgetc(pf);
    19. printf("%c\n",ch); //c
    20. ch = fgetc(pf);
    21. printf("%c\n",ch);//d
    22. //写文件
    23. // fputc('a',pf);
    24. // fputc('b',pf);
    25. // fputc('c',pf);
    26. // int i = 0;
    27. // for (i = 0; i < 26; i++)
    28. // {
    29. // fputc('a'+i,pf);
    30. // }
    31. //关闭文件
    32. fclose(pf);
    33. pf = NULL;
    34. return 0;
    35. }
    1. int main()
    2. {
    3. //相对路径
    4. //绝对路径
    5. ///Users/fan/Documents/c_study/c_test26/data.txt
    6. //打开文件
    7. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    8. if (pf == NULL)
    9. {
    10. perror("fopen");
    11. return 1;
    12. }
    13. //读文件
    14. // int ch = fgetc(pf);
    15. // printf("%c\n",ch); //a
    16. // ch = fgetc(pf);
    17. // printf("%c\n",ch); //b
    18. // ch = fgetc(pf);
    19. // printf("%c\n",ch); //c
    20. // ch = fgetc(pf);
    21. // printf("%c\n",ch);//d
    22. //写文件
    23. // fputc('a',pf);
    24. // fputc('b',pf);
    25. // fputc('c',pf);
    26. // int i = 0;
    27. // for (i = 0; i < 26; i++)
    28. // {
    29. // fputc('a'+i,pf);
    30. // }
    31. // fputs("hello fan\n",pf);
    32. // fputs("hello fanfan\n",pf);
    33. //读文件 - 读一行
    34. char arr[10] = {0};
    35. fgets(arr,15,pf);
    36. printf("%s\n",arr);
    37. fgets(arr,15,pf);
    38. printf("%s\n",arr);
    39. //关闭文件
    40. fclose(pf);
    41. pf = NULL;
    42. return 0;
    43. }
    1. struct S
    2. {
    3. int a;
    4. float s;
    5. };
    6. int main()
    7. {
    8. //相对路径
    9. //绝对路径
    10. ///Users/fan/Documents/c_study/c_test26/data.txt
    11. //打开文件
    12. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    13. if (pf == NULL)
    14. {
    15. perror("fopen");
    16. return 1;
    17. }
    18. //写文件
    19. //struct S s = {100,3.14f};
    20. //fprintf(pf,"%d %f",s.a,s.s);
    21. struct S s = {0};
    22. fscanf(pf,"%d %f",&(s.a),&(s.s));
    23. printf("%d %f",s.a,s.s);
    24. //关闭文件
    25. fclose(pf);
    26. pf = NULL;
    27. return 0;
    28. }
    1. struct S
    2. {
    3. int a;
    4. float s;
    5. char str[10];
    6. };
    7. int main()
    8. {
    9. struct S s = {99, 6.18f, "bit"};
    10. //打开文件
    11. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","wb");
    12. if (pf == NULL)
    13. {
    14. perror("fopen");
    15. return 1;
    16. }
    17. //写文件
    18. fwrite(&s,sizeof(struct S),1,pf);
    19. //关闭文件
    20. fclose(pf);
    21. pf = NULL;
    22. return 0;
    23. }
    1. int main()
    2. {
    3. struct S s = {0};
    4. //打开文件
    5. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","rb");
    6. if (pf == NULL)
    7. {
    8. perror("fopen");
    9. return 1;
    10. }
    11. //读文件
    12. fread(&s,sizeof(struct S),1,pf);
    13. printf("%d %f %s\n",s.a,s.s,s.str);
    14. //关闭文件
    15. fclose(pf);
    16. pf = NULL;
    17. return 0;
    18. }

     4. 文件的随机读写

     4.1 fseek

    根据文件指针的位置和偏移量来定位文件指针。

    int fseek ( FILE * stream, long int offset, int origin );

    1. int main()
    2. {
    3. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    4. if (pf == NULL)
    5. {
    6. perror("fopen");
    7. return 1;
    8. }
    9. //读文件
    10. //定位文件指针到f
    11. int ch = fgetc(pf);
    12. printf("%c\n",ch); //a
    13. ch = fgetc(pf);
    14. printf("%c\n",ch); //b
    15. ch = fgetc(pf);
    16. printf("%c\n",ch); //c
    17. ch = fgetc(pf);
    18. printf("%c\n",ch); //d
    19. fseek(pf,-3,SEEK_END);
    20. ch = fgetc(pf);
    21. printf("%c\n",ch);
    22. //关闭文件
    23. fclose(pf);
    24. pf = NULL;
    25. return 0;
    26. }

    4.2 ftell

    返回文件指针相对于起始位置的偏移量

    long int ftell ( FILE * stream );

    1. int main()
    2. {
    3. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    4. if (pf == NULL)
    5. {
    6. perror("fopen");
    7. return 1;
    8. }
    9. //读文件
    10. //定位文件指针到f
    11. int ch = fgetc(pf);
    12. printf("%c\n",ch); //a
    13. ch = fgetc(pf);
    14. printf("%c\n",ch); //b
    15. ch = fgetc(pf);
    16. printf("%c\n",ch); //c
    17. ch = fgetc(pf);
    18. printf("%c\n",ch); //d
    19. // fseek(pf,-3,SEEK_END);
    20. // ch = fgetc(pf);
    21. // printf("%c\n",ch);
    22. int pos = ftell(pf);
    23. printf("%d\n",pos); //4
    24. //关闭文件
    25. fclose(pf);
    26. pf = NULL;
    27. return 0;
    28. }

    4.3 rewind 

    让文件指针的位置回到文件的起始位置

    void rewind ( FILE * stream );

    1. int main()
    2. {
    3. FILE* pf = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    4. if (pf == NULL)
    5. {
    6. perror("fopen");
    7. return 1;
    8. }
    9. //读文件
    10. //定位文件指针到f
    11. int ch = fgetc(pf);
    12. printf("%c\n",ch); //a
    13. ch = fgetc(pf);
    14. printf("%c\n",ch); //b
    15. ch = fgetc(pf);
    16. printf("%c\n",ch); //c
    17. ch = fgetc(pf);
    18. printf("%c\n",ch); //d
    19. // fseek(pf,-3,SEEK_END);
    20. // ch = fgetc(pf);
    21. // printf("%c\n",ch);
    22. // int pos = ftell(pf);
    23. // printf("%d\n",pos); //4
    24. rewind(pf); //回到起始位置
    25. ch = fgetc(pf);
    26. printf("%c\n",ch); //a
    27. //关闭文件
    28. fclose(pf);
    29. pf = NULL;
    30. return 0;
    31. }

     5. 文本文件和二进制文件

    6. 文件读取结束的判断  

    6.1 被错误使用的feof

    牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。

    而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。

    1. 文本文件读取是否结束,判断返回值是否为EOF (fgetc),或者NULL(fgets)

    例如:

    fgetc判断是否为EOF.

    fgets判断返回值是否为NULL.

    2. 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。

    例如:

    fread判断返回值是否小于实际要读的个数。

    1. int main(void)
    2. {
    3. int c; // 注意:int,非char,要求处理EOF
    4. FILE *fp = fopen("test.txt", "r");
    5. if (!fp)
    6. {
    7. perror("File opening failed");
    8. return EXIT_FAILURE;
    9. }
    10. // fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
    11. while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
    12. {
    13. putchar(c);
    14. }
    15. // 判断是什么原因结束的
    16. if (ferror(fp))
    17. puts("I/O error when reading");
    18. else if (feof(fp))
    19. puts("End of file reached successfully");
    20. fclose(fp);
    21. }
    22. //拷贝文件
    23. //拷贝data.txt文件,产生一个新文件data1.txt
    24. int main()
    25. {
    26. FILE* pfRead = fopen("/Users/fan/Documents/c_study/c_test26/data.txt","r");
    27. if (pfRead == NULL)
    28. {
    29. perror("open file for read");
    30. return 1;
    31. }
    32. FILE* pfWrite = fopen("/Users/fan/Documents/c_study/c_test26/data2.txt","w");
    33. if (pfWrite == NULL)
    34. {
    35. perror("open file for write");
    36. fclose(pfRead);
    37. pfRead = NULL;
    38. return 1;
    39. }
    40. //读写文件
    41. int ch = 0;
    42. while((ch = fgetc(pfRead)) != EOF)
    43. {
    44. fputc(ch,pfWrite);
    45. }
    46. //关闭文件
    47. fclose(pfRead);
    48. pfRead = NULL;
    49. fclose(pfWrite);
    50. pfWrite = NULL;
    51. return 0;
    52. }

     7. 文件缓冲区

  • 相关阅读:
    redis主从复制原理
    -最低分-
    windows MYSQL安装与卸载
    多线程JUC 第2季 多线程的原子性
    Vue的props配置项
    超详细:ARM64 汇编实现 C 标准库中的 memset() 函数
    常用I/O复用模型 --> 一、单线程Accept(无IO复用)
    Nacos 下载安装
    流量染色SDK设计的思考
    企业和个人选择云桌面的云终端还是云服务器?看完本文就知道
  • 原文地址:https://blog.csdn.net/qq_61658398/article/details/133904451