关键词不能为空

当前您在: 主页 > 英语 >

模式C语言程序设计(第3版)何钦铭-颜-晖-第12章--文件

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-01-24 14:14
tags:

-

2021年1月24日发(作者:猪窝)


12



文件


【练习
12-1
】读出例
12-1
学生成绩文件内容,输出最高分和最低分 及相应的学
号和姓名。

解答:

#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(

printf(

exit(0);

}

fscanf(fp,
.score);

max=min=students[0].score;

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




fscanf(fp,
score);

if(max
max=students[i].score;

j=i;

}

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

min=students[i].score;

k=i;



}

}



printf(
score: %d,num:%d,name:%sn
ts[j].stname);

printf(
score: %d,num:%d,name:%sn
ts[k].stname);

if(fclose(fp)){

printf(

exit(0);

}

~

return 0;

}


【练习
12-2
】< br>请使用例
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(

printf(

exit(0);

}

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

printf(



scanf(

encrypt;

fprintf(fp,

}

if(fclose(fp)){

printf(



exit(0);

}



return 0;

}

void encrypt(char *pwd)

{

int i;


for(i=0;i
if(pwd[i]=='z')



pwd[i]='a';

else

pwd[i]+=1;

}


【练习
12-3
】 例
12-3
中为什么在执行
fputc

ch

f p2
)前要判断
ch
的值是否
等于
EOF
改写例
1 2-3
的程序,在复制用户信息文件后,再统计被复制文件中字
符的数量。

解答:

文件结束符
EOF
是一个值为
-1
的常量 ,读文件时可用来判断从文件中读入的字
符是否为
EOF
来决定循环是否继续。

~

#include<>

#include<>

int main(void)

{

FILE *fp1,*fp2;

char ch;

int count=0;


<

if((fp1=fopen(

printf(

exit(0);

}

if((fp2=fopen(

printf(

exit(0);

}

,

while(!feof(fp1)){

ch=fgetc(fp1);



if(ch!=EOF) {

fputc(ch,fp2);

count++;

}

}

if(fclose(fp1)){



printf(

exit(0);

}

if(fclose(fp2)){

printf(

exit(0);

}

printf(
中字符数量为:
%d

'



return 0;

}

【练习
12-4
】字母转换并统计行数
:
读取一个指定的文本文件 ,显示在屏幕上,
如果有大写字母,
则改成小写字母再输出,
并根据回车符统计行数。
试编写相应
程序。

解答:

#include<>

#include<>

int main(void)

?

{

char ch;

int countline=0;

FILE *fp;

if((fp=fopen(
练习


printf(

exit(0);

}



while(!feof(fp)){

ch=fgetc(fp);

if(ch!=EOF)

if(ch>='A'&&ch<='Z')

printf(

else

printf(

if(ch=='n')

<



countline++;

}

printf(

printf(

if(fclose(fp)){

printf(

exit(0);

}

>

return 0;

}


【练习
12-5
】写字符并验证
:
从键盘输入一行字符,写入到文 件中,并重新读
出,
最终在屏幕上显示验证。
程序输入以读到回车符“

n”为结束,
读文件时要

EOF
来控制循环。试编写相应程序。

解答:

#include<>

#include<>

int main(void)



{

FILE *fp;

char ch;

if((fp=fopen(

printf(

exit(0);

}

printf(



ch=getchar();

while(ch!='n'){

fputc(ch,fp);

ch=getchar();

}

rewind(fp);

while(!feof(fp)){


ch=fgetc(fp);

;

if(ch!=EOF)

putchar(ch);

}

printf(

if(fclose(fp)){


printf(




exit(0);

}

)

return 0;

}


【练习
12-6
】实数取整写入文件
:
文件中有若干个实数,请分 别读出,将每个
实数按四舍五入取整后存入文件中。试编写相应程序。

解答:

#include<>

#include<>

int main(void)

|

{

FILE *fp1,*fp2;

double a;

if((fp1=fopen(

printf(

exit(0);

}

if((fp2=fopen(

}

printf(

exit(0);

}

while(!feof(fp1)){

fscanf(fp1,

fprintf(fp2,

}

if(fclose(fp1)){

#

printf(

exit(0);

}

if(fclose(fp2)){

printf(

exit(0);

}

return 0;

|

}

【练习< br>12-7
】修改例
12-6,
增加修改资金账户的功能。输入一个记录
ID
,如果
文件中已存在该记录,
则输入新的记录信息并更新资金账户文件中相应记录 的信
息。要求定义和调用函数
UpdateLog
()
,其功能是修改资金账 户记录。



解答:

#include<>

#include<>

long size;

struct LogData{

long logid;



char logdate[11];

char 1ognote[15];

double charge;

double balance;

};

int inputchoice()

{

int mychoice;

)

printf(

printf(

printf(

scanf(

return mychoice;

}

long getLogcount(FILE *cfptr)

{



long begin,end,logcount;

fseek(cfptr,OL,SEEK_SET);

begin=ftell(cfptr);

fseek(cfptr,size,SEEK_END);

end=ftell(cfptr);

logcount=(end- begin)/size-1;

return logcount;

}



/*
列出所有收支流水账
*/

void ListAllLog(FILE *cfptr)

{

struct LogData log;

fseek(cfptr,OL,SEEK_SET);

fread(&log,size,1,cfptr);

printf(

while(!feof(cfptr)){

)

printf(



fread(&log,size,1,cfptr);

};

}

/*
查询显示最后一条记录
*/

void QueryLastLog(FILE *cfptr)

{

struct LogData log;

!

long logcount;

logcount=getlogcount(cfptr);

if(1ogcount>0)

{

fseek(cfptr,size*(logcount-1),SEEK_SET);

fread(&log,size,1,cfptr);

printf(

printf(

?

printf(

}

else printf(

}

/*
添加新记录
*/

void AddNewLog(FILE *cfptr)

{

struct LogData log,lastlog;



long logcount;

printf(

scanf(

printf(

printf(

scanf(

logcount=getLogcount(cfptr);


!

if(logcount>0){

fseek(cfptr,size*(logcount-1),SEEK_SET);

fread(&lastlog,size,1,cfptr)

=+1;

=+;

}

else{


=1;

|



=;

}

rewind(cfptr);ogid=last-taraetlastlog;

printf(

fwirte(&log,sizeof(struct LogData),1,cfptr);

}

/*
修改资金账户
*/

void UpdateLog(FILE *cfptr)



{

FILE *fpout;

struct LogData user;

char date[11];

char note[15];

double charge;

double balance;

int choice,ID;

-

cfptr=fileopen(

if((fpout=fopen(

printf(

exit(0);

}

printf(

scanf(

while(!feof(cfptr)){

~

fread(&user,sizeof(struct LogData),1,cfptr);

if(strcmp,ID)==0){

printf(
请输入修改信息
:n

printf(

scanf(

strcpy,date);

printf(

scanf(

.

strcpy,note);

printf(

=charge;

printf(

scanf(

=balance;

fwrite(&user,sizeof(struct LogData),1,fpout);

}

%

-


-


-


-


-


-


-


-



本文更新与2021-01-24 14:14,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/561063.html

C语言程序设计(第3版)何钦铭-颜-晖-第12章--文件的相关文章

C语言程序设计(第3版)何钦铭-颜-晖-第12章--文件随机文章