C语言文件读写的操作

1.文件打开

FILE *fp=fopen("dat.dat","w");

第一个参数可以是绝对路径,也可以是相对路径。

用相对路劲要和源程序在一个文件夹下

绝对路径要用两个反斜杠,不然会有歧义

第二个参数是打开文件的方式

运行后,会在路径的目录下生成两个文件

 

2.文件关闭

 fclose(fp);

这个函数会清空缓存区的数据

 

3.读写文件

  • 以字符形式读写
fputc(ch,fp);

 函数 fputc() 把参数 c 的字符值写入到 fp 所指向的输出流中。如果写入成功,它会返回写入的字符,如果发生错误,则会返回 EOF

FILE *fp=fopen("dat.dat","w");
fputc('a',fp);
fclose(fp);
FILE *fp=fopen("dat.dat","r");
char ch=fgetc(fp);
printf("%c",ch);

从键盘输入一行字符写入文件

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    if((fp=fopen("dat.dat","w"))==NULL)//判断文件是否能正常打开
    {
        printf("Open error!");
        exit(0);
    }
    char ch;
    while((ch=getchar())!='\n')//每次从键盘读取一个字符,写入文件
    {
        fputc(ch,fp);
    }
    fclose(fp);
    return 0;
}

 

读取文件的内容,打印在屏幕上

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    if((fp=fopen("dat.dat","r"))==NULL)
    {
        printf("Open error!");
        exit(0);
    }
    char ch;
    while((ch=fgetc(fp))!=EOF)//读到文件末尾时停止
    {
        printf("%c",ch);
    }
    return 0;
}

 

  • 以字符串形式读写
#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    if((fp=fopen("dat.dat","a"))==NULL)
    {
        printf("Open error!");
        exit(0);
    }
    char ch[20];
    gets(ch);
    fputs(ch,fp);//将字符串写入
    fclose(fp);
    return 0;
}

读取到的字符串会在末尾自动加上'\0',因此实际上只读到了n-1个字符

fgets(str,n,fp);有局限性,遇到换行会停下,每次只能读取一行

  • 以数据块形式读写

 读取多行内容需要使用 fwrite(*ptr,size,n,fp); 相应地写入为 fread(*ptr,size,n,fp);

ptr为内存中地指针,可以为数组、变量、结构体等;

#include<stdio.h>
#include<stdlib.h>
//输入一个数组,写入文件再读取
int main()
{
    int a[10],b[10];
    for(int i=0;i<10;i++)
        scanf("%d",&a[i]);
    FILE *fp;
    if((fp=fopen("dat.dat","w"))==NULL)
    {
        printf("Open error!");
        exit(0);
    }
    fwrite(a,sizeof(int),10,fp);
    rewind(fp);//将文件位置读写标记移到最开头
    fclose(fp);
    fp=fopen("dat.dat","r");
    fread(b,sizeof(int),10,fp);
    for(int i=0;i<10;i++)
        printf("%d ",b[i]);
    fclose(fp);
    return 0;
}

文件打开为乱码,数组会以二进制地方式存入。

#include<stdio.h>
#include<stdlib.h>
//生成学生成绩数据写入文件,按照平均分从高到低排序读取文件
struct Student{
    long num;
    int score[5];
    float average;
};
int comp(const void *p1,const void *p2)
{
    return (*(struct Student*)p1).average<(*(struct Student*)p2).average?1:-1;
}
int main()
{
    struct Student stud[10],stud1[10];
    srand((unsigned int)time(NULL));
    for(int i=0;i<10;i++)//生成分数
    {
        stud[i].average=0;
        for(int j=0;j<5;j++)
        {
            stud[i].score[j]=rand()%(100-60+1)+60;
            stud[i].average+=stud[i].score[j];
        }
        stud[i].average/=5;
    }

    for(int i=0;i<10;i++)//生成学号
        stud[i].num=19020001+i;
    for(int i=0;i<10;i++)//排序前
    {
        printf("%ld:",stud[i].num);
        for(int j=0;j<5;j++)
        {
            printf("%d ",stud[i].score[j]);
        }
        printf("%.2f ",stud[i].average);
        putchar(10);
    }

    FILE *fp;
    if((fp=fopen("stud.dat","wb"))==NULL)//创建文件
        printf("Open error!\n");
    for(int i=0;i<10;i++)
    {
        fwrite(&stud[i],sizeof(struct Student),1,fp);//写入文件
    }
    fclose(fp);//关闭文件
    //读入文件
    fp=fopen("stud.dat","rb");
    for(int i=0;i<10;i++)
    {
        fread(&stud1[i],sizeof(struct Student),1,fp);//写入文件
    }
    qsort(stud1,10,sizeof(stud[0]),comp);//按平均分排序
    for(int i=0;i<10;i++)
    {
        printf("%ld %.2f\n",stud1[i].num,stud1[i].average);
    }
    return 0;
}
View Code
  • 格式化读写

 fscanf()、fprintf()    和scanf()、printf()功能类似,前者操作对象是磁盘文件,后者操作对象是标准输入输出设备。

格式为:

fscanf(fp,格式控制字符串,地址表列)
fprintf(fp,格式控制字符串,输出表列)
  • 随机读写

 移动文件位置指针的函数 rewind(fp)、fseek(fp,偏移量,基点)

 参考:http://c.biancheng.net/view/2075.html

 

4.feof(fp)函数

测试是否到达文件末尾,但不引起读写动作

 https://blog.csdn.net/qq_33706673/article/details/78308466

posted @ 2020-12-25 17:39  inss!w!  阅读(661)  评论(0编辑  收藏  举报