c语言程序设计何钦铭颜晖pdf,C语言程序设计(第3版)何钦铭颜晖第12章文件.pdf

第 12 章 文件

【练习 12-1 】读出例 12-1 学生成绩文件 f12-1.txt 内容,输出最高分和最低分

及相应的学号和姓名。

解答:

#include

#include

struct student{

long num;

char stname[20];

int score;

};

int main(void)

{

FILE *fp;

int i,max,min,j=0,k=0;

struct student students[5];

if((fp=fopen("f12-1.txt","r"))==NULL) {

printf("File open error!\n");

exit(0);

}

fscanf(fp,"%ld%s%d",&students[0].num,students[0].stname,&students[0]

.score);

max=min=students[0].score;

for(i=1;i<=4;i++){

fscanf(fp,"%ld%s%d",&students[i].num,students[i].stname,&students[i].

score);

if(max

max=students[i].score;

j=i;

}

if(min>students[i].score){

min=students[i].score;

k=i;

}

}

printf("Max

score: %d,num:%d,name:%s\n",students[j].score,students[j].num,&studen

ts[j].stname);

printf("Min

score: %d,num:%d,name:%s\n",students[k].score,students[k].num,&studen

ts[k].stname);

if(fclose(fp)){

printf("Can not close the file!\n");

exit(0);

}

return 0;

}

【练习12-2 】请使用例 8-9 答电码加密函数对民吗字符串进行加密, 改写例 12-2 。

解答:

#include

#include

#include

struct sysuser{

char username[20];

char password[8];

};

void encrypt(char *pwd);

int main(void)

{

FILE *fp;

int i;

struct sysuser su;

if((fp=fopen("f12-2.txt","w"))==NULL){

printf("File open error!\n");

exit(0);

}

for(i=1;i<=5;i++){

printf("Enter %dth sysuser (name password):",i);

scanf("%s%s",su.username,su.password);

encrypt(su.password);

fprintf(fp,"%s %s\n",su.username,su.password);

}

if(fclose(fp)){

printf("Can not close the file!\n");

exit(0);

}

return 0;

}

void encrypt(char *pwd)

{

int i;

for(i=0;i

if(pwd[i]=='z')

pwd[i]='

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值