一文搞懂C语言文件操作

一、文件分类

根据数据的组织形式,可以将文件分为文本文件和二进制文件。通俗讲,文本文件就是你能看懂的,而二进制文件是你看不懂的!

二、文件打开与关闭

假设已经定义了一个文件指针

FILE *fp;

有了文件指针,意味着你就可以对文件为所欲为。

  • 用到的函数
    • fp = fopen(文件名,打开模式);
    • fclose(fp);
      打开模式详见小甲鱼学习网站fopen函数详解,讲的非常清楚。
  • 案例
int main()
{
    
    
    FILE *fp;
    if((fp = fopen("E:\\data.txt","r")) == NULL){
    
    
        printf("文件打开失败!\n");
        exit(EXIT_FAILURE);
    }
    int ch;
    while((ch = getc(fp)) != EOF){
    
    
        putchar(ch);
    }
    fclose(fp); /*文件有打开,必须要有关闭*/
    return 0;
}

三、文件顺序读写

3.1 读取写入字符

  • 用到的函数
    • ch = fgetc(fp)
      • ch:你从文件中读出的字符
      • fp:从哪个文件中读
    • fputc(ch,fp)
      • ch:表示往文件中要写入的字符
      • fp:文件指针,表示要往哪个文件中写入
  • 案例
    将一个文件中的内容拷贝到另一个文件中
int main()
{
    
    
    FILE *fp1;
    FILE *fp2;
    int ch;
    if((fp1 = fopen("E:\\data.txt","r")) == NULL){
    
    
        printf("文件打开失败!\n");
        exit(EXIT_FAILURE);
    }
    if((fp2 = fopen("E:\\copy.txt","w")) == NULL){
    
    
        printf("文件打开失败!\n");
        exit(EXIT_FAILURE);
    }
    while((ch = fgetc(fp1)) != EOF){
    
    
        fputc(ch,fp2);
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}

3.2 读取写入字符串

  • 用到的函数
    • fgets(str,n,fp)
      • str:读出的字符串存放再str中
      • n:要读到的字符个数,包括字符串结束符,即只能读取n-1个字符
      • fp:从fp指向文件中读取
      • 返回值:str的首地址
    • fputs(str,fp)
      • str:往fp所指文件中写入的字符串地址
      • fp:往fp指向文件中写入
      • 返回值:若写入成功,返回0;否则,返回EOF
  • 案例1
    往一个文件中先写入字符串,然后再读取出来。
int main()
{
    
    
    FILE *fp;
    char buffer[N];
    if((fp = fopen("E:\\test.txt","w")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    /*往文件中写入*/
    fputs("I love FishC.com\n",fp);
    fputs("I love China\n",fp);
    fputs("I love my parent",fp);
    fclose(fp);
    if((fp = fopen("E:\\test.txt","r")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    /*读取*/
    while(!feof(fp)){
    
    
        fgets(buffer,N,fp);
        printf("%s\n",buffer);
    }
    return 0;
}
  • 案例2
int main()
{
    
    
    FILE *fp;
    char buffer[N];
    if((fp = fopen("E:\\test.txt","w")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    /*往文件中写入*/
    fputs("I love FishC.com\n",fp);
    fputs("I love China\n",fp);
    fputs("I love my parent\n",fp);  //出问题的一行
    fclose(fp);
    if((fp = fopen("E:\\test.txt","r")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    /*读取*/
    while(!feof(fp)){
    
    
        fgets(buffer,N,fp);
        printf("%s\n",buffer);
    }
    return 0;
}

NOTE:
若在读取字符中遇到EOF,则eof指示器被设置;但是如果再没有读到任何字符之前就遇到了EOF,则str指示器指向的位置保持原来的位置不变,即“I love my parent”被读取了两次。

3.3 读取写入一组数据

  • 用到的函数
    • fread(buffer,size,count,fp)
      • buffer:读取到的数据的存放地址
      • size:要读取的字节数
      • count:要读取多少个size字节的数据项
      • fp:从fp指向文件中读取
    • fwrite(buffer,size,count,fp)
      • buffer:要写入的数据的存放地址
      • size:要读取的字节数
      • count:要写入多少个size字节的数据项
      • fp:往fp指向文件中写入
  • 案例
    先向一个文件中吸入一组数据,然后再读出。
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Stu
{
    
    
    int num;
    char name[24];
    float score;
}stu[N];
int main()
{
    
    
    FILE *fp;
    int i;
    struct Stu temp;
    for(i = 0;i < N;i++){
    
    
        scanf("%d %s %f",&stu[i].num,stu[i].name,&stu[i].score);
    }
    if((fp = fopen("E:\\test1.txt","wb")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    for(i = 0;i < N;i++){
    
    
        if(fwrite(&stu[i],sizeof(struct Stu),1,fp) != 1){
    
    
            printf("写入文件失败\n");
        }
    }
    fclose(fp);
    /*读取文件*/
    if((fp = fopen("E:\\test1.txt","rb")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    for(i=0;i<N;i++){
    
    
        fread(&temp,sizeof(struct Stu),1,fp);
        printf("num = %d,name = %s,socre = %.2f\n",temp.num,temp.name,temp.score);
    }
    fclose(fp);
    return 0;
}

四、文件随机读写

  • 用到的函数
    • fseek(文件指针,位移量,起始点)
      • 文件指针:再哪个文件中进行操作
      • 位移量:从起始点开始,位置指针移动多少
      • 起始点:从哪个位置开始
    • rewind()
      • 使位置指针重新返回文件的开头
    • ftell()
      • 得到位置指针的当前位置,用相对于文件开头的位移量来表示
  • 案例
    先向文件中写入4个学生的数据,然后读取第2个学生的数据。
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct Stu
{
    
    
    int num;
    char name[24];
    float score;
}stu[N],somebody;
int main()
{
    
    
    FILE *fp;
    int i;
    printf("请输入学生的相关信息\n");
    for(i = 0;i < N;i++){
    
    
        scanf("%d %s %f",&stu[i].num,stu[i].name,&stu[i].score);
    }
    if((fp = fopen("E:\\text2.txt","w")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    for(i=0;i<N;i++){
    
    
        if(fwrite(&stu[i],sizeof(struct Stu),1,fp) != 1){
    
    
            printf("文件打开失败\n");
        }
    }
    fclose(fp);
    if((fp = fopen("E:\\text2.txt","r")) == NULL){
    
    
        printf("文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    printf("当前位置指针:%d\n",ftell(fp));
    fseek(fp,sizeof(struct Stu),0);
    fread(&somebody,sizeof(struct Stu),1,fp);
    printf("num = %d, name = %s, score = %f",somebody.num,somebody.name,somebody.score);
    fclose(fp);
    return 0;
}

五、其它函数

其他函数的详细讲解,请到小甲鱼(小甲鱼牛逼)的网站上自行学习。函数详解

猜你喜欢

转载自blog.csdn.net/dongjinkun/article/details/105114199
今日推荐