C 文件 书上练习

对函数feof()有疑惑时,可以再看一遍慕课这一节
1.按字符读写文件
(1)读写文件中的字符

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

int main()
{
    FILE *fp;
    char ch;
    int i;
    if((fp=fopen("demo.bin","wb"))==NULL){
        printf("failure to open demo.bin!\n");
        exit(1);
    }
    for(i=0;i<128;i++){
        fputc(i,fp);
    }
    fclose(fp);
    if((fp=fopen("demo.bin","rb"))==NULL){
        printf("failure to open demo.bin!\n");
        exit(1);
    }
    while((ch=fgetc(fp))!=EOF){
        if(isprint(ch)){
            printf("%c\t",ch);
        }else{
            printf("%d\t",ch);
        }
    }
    fclose(fp);
    return 0;
}

(2)读写文件中的字符串

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

2.按数据块读写文件

#include <stdio.h>
#include <stdlib.h>
#define N 30
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    long id;
    char name[10];
    char sex;
    DATE birthday;
    int score[4];
    float aver;
}STUDENT;
void inputscore(STUDENT stu[],int n,int m);
void averscore(STUDENT stu[],int n,int m);
void writetofile(STUDENT stu[],int n);
int readfromfile(STUDENT stu[]);
void print(STUDENT stu[],int n,int m);
int main()
{
    STUDENT stu[N];
    int n;
    printf("how many students?");
    scanf("%d",&n);
    inputscore(stu,n,4);
    averscore(stu,n,4);
    writetofile(stu,n);
    n=readfromfile(stu);
    print(stu,n,4);
    return 0;
}

void inputscore(STUDENT stu[],int n,int m){
    int i,j;
    for(i=0;i<n;i++){
        printf("input record %d: \n",i+1);
        scanf("%ld",&stu[i].id);
        scanf("%s",stu[i].name);
        scanf(" %c",&stu[i].sex);
        scanf("%d",&stu[i].birthday.year);
        scanf("%d",&stu[i].birthday.month);
        scanf("%d",&stu[i].birthday.day);

        for(j=0;j<4;j++){
            scanf("%d",&stu[i].score[j]);
        }
    }
}

void averscore(STUDENT stu[],int n,int m){
    int i,j,sum;

    for(i=0;i<n;i++){
        sum=0;
        for(j=0;j<m;j++){
            sum+=stu[i].score[j];
        }
        stu[i].aver=(float)sum/m;
    }
}

void writetofile(STUDENT stu[],int n){
    FILE *fp;
    if((fp=fopen("score.txt","w"))==NULL){
        printf("error");
        exit(1);
    }

    fwrite(stu,sizeof(STUDENT),n,fp);

    fclose(fp);
}

int readfromfile(STUDENT stu[]){
    FILE *fp;
    int i;
    if((fp=fopen("score.txt","r"))==NULL){
        printf("error");
        exit(1);
    }
    for(i=0;!feof(fp);i++){
        fread(&stu[i],sizeof(STUDENT),1,fp);
    }
    fclose(fp);
    printf("total student is %d\n",i-1);
    return i-1;
}

void print(STUDENT stu[],int n,int m){
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            printf("%4d",stu[i].score[j]);
        }
        printf("\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值