第十三章、文件操作

13.1二进制文件和文本文件

文本文件:每一位数字作为一个字符以ASCII的形式存储的,每一个数字占用一个字节的存储空间。
二进制文件:把整个数字作为一个二进制数来存储。

文件的打开和关闭:
FILE *fopen(const char *filename,const char *mode);
第一个参数表示文件名,包含路径和文件名两个部分。
第二参数表示打开方式
“r”:只读方式打开文本文件
“w”:只写方式,创建并打开文本文件,已存在的文件将被覆盖
“a”:只写方式,打开文本文件,向文件尾部添加数据。
“+”:与上面的字符串组合,表示以读写的方式打开文本文件
“b”:与上面的字符串组合,表示打开二进制文件

FILE *fp;
fp=fopen(“D:\demo.bin”,“ab+”);
fclose(fp);
文件打开后记得关闭:int fclose(FILE *fp);关闭成功,返回0值;否则返回非0值。

13.2读写文件

//按字符读写文件
int fgetc(FILE *fp);//从fp文件中读取一个字符,并将指针指向下一个字符。读取成功,返回该字符;否则返回EOF。

int fputc(int c,FILE*fp);//将字符C写入fp文件中,写入错误,返回EOF;否则返回字符c。

//写入字符到文件中
#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE *fp;
    char ch;
    if((fp=fopen("demo.txt","w"))==NULL)//看是否打开成功
    {
        exit(0);
    }
    ch=getchar();
    while(ch!='\n')  //以回车符作为文件的结尾
    {
        fputc(ch,fp);
        ch=getchar();//输入字符到缓冲区,直到键入回车符才从缓冲区逐个读出并赋值给ch。
    }
    fclose(fp);
    //从文件中读出字符
    if((fp=fopen("demo.txt","rb"))==NULL)
    {
        exit(0);
    }
    /*
    ch=fgetc(fp);
    while(!feof(fp)) feof()来判断是否读到文件末尾,是返回非0值
    {
     putchar(ch);
     ch=fgetc(fp);
     if(isprint(ch))  判断是否为可打印字符
     {
      printf("%c\n",ch);
     }
    }
    */
    while((ch=fgetc(fp))!=EOF)
    {
        putchar(ch);
    }
    fclose(fp);
    return 0;
}
//读写文件中的字符串
char *fgets(char *s,int n,FILE *fp);
//从fp中读取n-1个字符,在字符串的末尾加上'\0',存入s。读取失败,返回空指针。
int fputs(const char *s,FILE *fp);
//fgets与gets不同,会将换行符也读入;fputs与puts不同,不会在写入文件的结尾加上换行符。

#include<stdio.h>
#include<stdlib.h>
#define N 80
int main()
{
    FILE *fp;
    char str[N];
    if((fp=fopen("demo.txt","a"))==NULL)
    {
        exit(0);
    }
    gets(str);
    fputs(str,fp);
    fclose(fp);
    

//按格式读写文件
int fscanf(FILE *fp,const char *format,…);
//第一个参数是文件指针,第二个参数为格式控制参数,第三个参数为地址参数表列,后两个参数与scanf()相同.从文件读数据。
int fprintf(FILE *fp,const char *format,…);

//向文件写入数据
#include<stdio.h>
#include<stdlib.h>
#define N 30
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    long studentID;
    char studentName[10];
    char studentSex;
    DATE birthday;
    int score[4];
    float aver;
}STUDENT;
void InputScore(STUDENT stu[],int n,int m)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%ld",&stu[i].studentID);
        scanf("%s",stu[i].studentName);
        scanf("% c",&student[i].studentSex);//前面有一个空格
        scanf("%d",&stu[i].birthday.year);
        scanf("%d",&stu[i].birthday.month);
        scanf("%d",&stu[i].birthday.day);
        for(j=0;j<3;j++)
        {
            scanf("%d",&stu[i].score[j]);
        }
    }
}
void AverScore(STUDENT stu[],int n,int m)
{
    int i,j;
    float sum;
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<m;j++)
        {
            sum+=stu[i].score[j];
        }
        stu[i].aver=sum/m;
    }
}

void WritetoFile(STUDENT stu[],int n,int m)
{
    FILE *fp;
    int i,j;
    if((fp=fopen("score.txt","w"))==NULL)
    {
        exit(0);
    }
    for(i=0;i<n;i++)
    {
    fprintf(fp,"%ld%s%c%d%d%d",stu[i].studentID,stu[i].studentName,student[i].studentSex,stu[i].birthday.year,stu[i].birthday.month,stu[i].birthday.day);
        for(j=0;j<m;j++)
        {
            fprintf(fp,"%d",stu[i].score[j]);
        }
        fprintf(fp,"%lf",stu[i].aver);
    }
    fclose(fp);
}
void ReadfromFile(STUDENT stu[],int n,int m)
{
    FILE *fp;
    int i,j;
    if((fp=fopen("score.txt","r"))==NULL)
    {
        exit(0);
    }
    for(i=0;i<n;i++)
    {
        fscanf(fp,"%ld",&stu[i].studentID);
        fscanf(fp,"%s",stu[i].studentName);
        fscanf(fp,"% c",&student[i].studentSex);//前面有一个空格
        fscanf(fp,"%d",&stu[i].birthday.year);
        fscanf(fp,"%d",&stu[i].birthday.month);
        fscanf(fp,"%d",&stu[i].birthday.day);
        for(j=0;j<3;j++)
        {
            fscanf(fp,"%d",&stu[i].score[j]);
        }
    }
}
int main()
{
    STUDENT stu[N];
    int n;
    scanf("%d",&n);
    InputScore(stu,n,4);
    AverScore(stu,n,4);
    WritetoFile(stu,n,4);
    ReadfromFile(stu,n,4);
    return 0;
}

//按数据块读写文件
unsigned int fread(void *buffer,unsigned int size,unsigned int count,FILE *fp);
//从fp中读取数据块到buffer指向的内存中,size是每个数据块的大小,count最多允许数据块读取的个数
unsigned int fwrite(const void *buffer,unsigned int size,unsigned int count,FILE *fp);
//将buffer指向内存中的数据块写入fp指向的文件中。函数的返回值也就是实际写入的数据块个数
void WritetoFile(STUDENT stu[],int n,int m)
{
    FILE *fp;
    int i,j;
    if((fp=fopen(“score.txt”,“w”))==NULL)
    {
        exit(0);
    }
    fwrite(stu,sizeof(STUDENT),n,fp);
    fclose(fp);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

身影王座

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值