c语言程序设计何钦铭颜晖第12章文件.docx

上传人:b****5 文档编号:7522984 上传时间:2023-05-11 格式:DOCX 页数:24 大小:20.66KB
下载 相关 举报
c语言程序设计何钦铭颜晖第12章文件.docx_第1页
第1页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第2页
第2页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第3页
第3页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第4页
第4页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第5页
第5页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第6页
第6页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第7页
第7页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第8页
第8页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第9页
第9页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第10页
第10页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第11页
第11页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第12页
第12页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第13页
第13页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第14页
第14页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第15页
第15页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第16页
第16页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第17页
第17页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第18页
第18页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第19页
第19页 / 共24页
c语言程序设计何钦铭颜晖第12章文件.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c语言程序设计何钦铭颜晖第12章文件.docx

《c语言程序设计何钦铭颜晖第12章文件.docx》由会员分享,可在线阅读,更多相关《c语言程序设计何钦铭颜晖第12章文件.docx(24页珍藏版)》请在冰点文库上搜索。

c语言程序设计何钦铭颜晖第12章文件.docx

c语言程序设计何钦铭颜晖第12章文件

第12章文件

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

解答:

#include<>

#include<>

structstudent{

longnum;

charstname[20];

intscore;

};

intmain(void)

{

FILE*fp;

inti,max,min,j=0,k=0;

structstudentstudents[5];

if((fp=fopen("","r"))==NULL){

printf("Fileopenerror!

\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("Maxscore:

%d,num:

%d,name:

%s\n",students[j].score,students[j].num,&students[j].stname);

printf("Minscore:

%d,num:

%d,name:

%s\n",students[k].score,students[k].num,&students[k].stname);

if(fclose(fp)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

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

解答:

#include<>

#include<>

#include<>

structsysuser{

charusername[20];

charpassword[8];

};

voidencrypt(char*pwd);

intmain(void)

{

FILE*fp;

inti;

structsysusersu;

if((fp=fopen("","w"))==NULL){

printf("Fileopenerror!

\n");

exit(0);

}

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

printf("Enter%dthsysuser(namepassword):

",i);

scanf("%s%s",,;

encrypt;

fprintf(fp,"%s%s\n",,;

}

if(fclose(fp)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

voidencrypt(char*pwd)

{

inti;

for(i=0;i

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

pwd[i]='a';

else

pwd[i]+=1;

}

【练习12-3】例12-3中为什么在执行fputc(ch,fp2)前要判断ch的值是否等于EOF改写例12-3的程序,在复制用户信息文件后,再统计被复制文件中字符的数量。

解答:

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

#include<>

#include<>

intmain(void)

{

FILE*fp1,*fp2;

charch;

intcount=0;

if((fp1=fopen("","r"))=NULL){

printf("Fileopenerror!

\n");

exit(0);

}

if((fp2=fopen("","w"))==NULL){

printf("Fileopenerror!

\n");

exit(0);

}

while(!

feof(fp1)){

ch=fgetc(fp1);

if(ch!

=EOF){

fputc(ch,fp2);

count++;

}

}

if(fclose(fp1)){

printf("Cannotclosethefile!

\n");

exit(0);

}

if(fclose(fp2)){

printf("Cannotclosethefile!

\n");

exit(0);

}

printf("f12-2中字符数量为:

%d",count);

return0;

}

【练习12-4】字母转换并统计行数:

读取一个指定的文本文件,显示在屏幕上,如果有大写字母,则改成小写字母再输出,并根据回车符统计行数。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

charch;

intcountline=0;

FILE*fp;

if((fp=fopen("练习","r"))==NULL){

printf("Notopen!

");

exit(0);

}

while(!

feof(fp)){

ch=fgetc(fp);

if(ch!

=EOF)

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

printf("%c",ch-'A'+'a');

else

printf("%c",ch);

if(ch=='\n')

countline++;

}

printf("\n");

printf("file'slineis:

%d.",countline+1);

if(fclose(fp)){

printf("Cannotclose!

");

exit(0);

}

return0;

}

【练习12-5】写字符并验证:

从键盘输入一行字符,写入到文件中,并重新读出,最终在屏幕上显示验证。

程序输入以读到回车符“\n”为结束,读文件时要用EOF来控制循环。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp;

charch;

if((fp=fopen("","w+"))==NULL){

printf("cannotopenfile!

");

exit(0);

}

printf("Inputthestring:

\n");

ch=getchar();

while(ch!

='\n'){

fputc(ch,fp);

ch=getchar();

}

rewind(fp);

while(!

feof(fp)){

ch=fgetc(fp);

if(ch!

=EOF)

putchar(ch);

}

printf("\n");

if(fclose(fp)){

printf("cannotclosefile!

\n");

exit(0);

}

return0;

}

【练习12-6】实数取整写入文件:

文件中有若干个实数,请分别读出,将每个实数按四舍五入取整后存入文件中。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp1,*fp2;

doublea;

if((fp1=fopen("","r"))==NULL){

printf("Fileopenerror!

\n");

exit(0);

}

if((fp2=fopen("","w"))==NULL){

printf("Fileopenerror!

\n");

exit(0);

}

while(!

feof(fp1)){

fscanf(fp1,"%lf",&a);

fprintf(fp2,"%.0f",a);

}

if(fclose(fp1)){

printf("Cannotclosethefile!

\n");

exit(0);

}

if(fclose(fp2)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

【练习12-7】修改例12-6,增加修改资金账户的功能。

输入一个记录ID,如果文件中已存在该记录,则输入新的记录信息并更新资金账户文件中相应记录的信息。

要求定义和调用函数UpdateLog(),其功能是修改资金账户记录。

解答:

#include<>

#include<>

longsize;

structLogData{

longlogid;

charlogdate[11];

char1ognote[15];

doublecharge;

doublebalance;

};

intinputchoice()

{

intmychoice;

printf("\nEnteryourchoice:

\n");

printf("1-AddanewcashLOG.\n2-ListAllCashLOG.\n");

printf("3-QueryLastCashLoG.\n0-Endprogram.\n");

scanf("%d",&mychoice);

returnmychoice;

}

longgetLogcount(FILE*cfptr)

{

longbegin,end,logcount;

fseek(cfptr,OL,SEEK_SET);

begin=ftell(cfptr);

fseek(cfptr,size,SEEK_END);

end=ftell(cfptr);

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

returnlogcount;

}

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

voidListAllLog(FILE*cfptr)

{

structLogDatalog;

fseek(cfptr,OL,SEEK_SET);

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

printf("logidlogdatelognotechargebalance\n");

while(!

feof(cfptr)){

printf("%6ld%-11s%-15%%\n",,,,,;

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

};

}

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

voidQueryLastLog(FILE*cfptr)

{

structLogDatalog;

longlogcount;

logcount=getlogcount(cfptr);

if(1ogcount>0)

{

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

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

printf("Thelastlogis:

\n");

printf("logid:

%-6ld\nlogdate:

%-11s\nlognote:

%-15s\n",,,;

printf("charge:

%\nbalance:

\n",,;

}

elseprintf("nologsinfile!

\n");

}

/*添加新记录*/

voidAddNewLog(FILE*cfptr)

{

structLogDatalog,lastlog;

longlogcount;

printf("Inputlogdate(format:

2006-01-01):

");

scanf("%s",;

printf("Inputlognote:

");scanf("%s",;

printf("InputCharge:

Income+andepend-:

");

scanf("%lf",&;

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("logid=%ld\n",;

fwirte(&log,sizeof(structLogData),1,cfptr);

}

/*修改资金账户*/

voidUpdateLog(FILE*cfptr)

{

FILE*fpout;

structLogDatauser;

chardate[11];

charnote[15];

doublecharge;

doublebalance;

intchoice,ID;

cfptr=fileopen("r");

if((fpout=fopen("","w"))==NULL){

printf("cannotopenthefile!

\n");

exit(0);

}

printf("EnterLogID:

");

scanf("%d",&ID);

while(!

feof(cfptr)){

fread(&user,sizeof(structLogData),1,cfptr);

if(strcmp,ID)==0){

printf("请输入修改信息:

\n");

printf("Date:

");

scanf("%s",date);

strcpy,date);

printf("Note:

");

scanf("%s",note);

strcpy,note);

printf("Charge:

");

=charge;

printf("Balance:

");

scanf("%s",&balance);

=balance;

fwrite(&user,sizeof(structLogData),1,fpout);

}

else

fwrite(&user,sizeof(structLogData),1,fpout);

}

if(fclose(cfptr)){

printf("cannotclosefile!

\n");

exit(0);

}

if(fclose(fpout)){

printf("cannotclosefile!

\n");

exit(0);

}

unlink("");;break;

}

}

if(fclose(fp)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

习题12

1、选择题

1.以下语句将输出____B_____。

#include<>

printf("%d%d%d",NULL,‘\0',EOF);

010-1EOF0EOF

2.如果二进制文件已经存在,现在要写入全新数据,应以____B____方式打开。

A."w"B."wb"C."w+"D.“wb+"

3.定义”FILE*fp;",则文件指针fp指向的是_____D_____。

A.文件在磁盘上的读写位置B文件在级冲区上的读写位置

C.整个磁盘文件D.文件类型结构

4.缓冲文件系统的文件缓冲区位于____C______。

A.磁盘缓冲区中B.磁盘文件中

C.内存数据区中D.程序文件中

5.使文件指针重新定位到文件读写的首地址的函数是_____C_____。

()()()()

二、填空题

1.函数fopen()的返回值是_指向文件缓冲区的首地址的文件结构类型指针_。

2.文件的三大特征是__名称__、__大小__和__类型__。

3.缓冲文件系统与非缓冲文件系统的不同点在于__系统是否为文件自动分配一块文件缓冲区(内存单元)__。

4.只能向指定文件写入一个字符的函数是_____fputc()函数______。

5.判断文件指针是否已经到了文件尾部的函数是___feof()函数___。

6.阅读程序,以下程序完成的功能是__文件infile内容复制到文件__。

#include<>

intmain(void)

{charinfile[10],outfile[10];

FILE*fpa,*fpb;

gets(infile);

gets(outfile);

fpa=fopen(infile,"r");

fpb=fopen(outfile,"w");

while(!

feof(fpa))

fputc(fgetc(fpa),fpb);

fclose(fpa);

fclose(fpb);

return0;

}

三、程序设计题

1.统计文本文件中各类字符个数:

分别统计一个文本文件中字母、数字及其他字符的个数。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp;

charch;

intletter=0,digiter=0,other=0;

if((fp=fopen("","r"))==NULL){

printf("Fileopenerror!

\n");

exit(0);

}

while((ch=fgetc(fp))!

=EOF){

if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')

letter++;

elseif(ch>='0'&&ch<='9')

digiter++;

else

other++;

}

printf("letter=%d,digiter=%d,other=%d",letter,digiter,other);

if(fclose(fp)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

2.将实数写入文件:

从键盘输人若干实数(以特殊数值-1结束),分别写到一个文本文件中。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp;

intnumber;

if((fp=fopen("","w"))==NULL){

printf("cannotopenfile!

\n");

exit(0);

}

printf("Inputnumber:

\n");

scanf("%d",&number);

while(number!

=-1){

fprintf(fp,"%d",number);

scanf("%d",&number);

}

if(fclose(fp)){

printf("Cannotclosethefile!

\n");

exit(0);

}

return0;

}

3.比较两个文本文件是否相等:

比较两个文本文件的内容是否相同,并输出两个文件中第一次出现不同字符内容的行号及列值。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp1,*fp2;

inti=1,j=1;

charch1,ch2;

if((fp1=fopen("","r"))==NULL){

printf("cannotopenfile!

\n");

exit(0);

}

if((fp2=fopen("","r"))==NULL){

printf("cannotopenfile!

\n");

exit(0);

}

ch1=fgetc(fp1);

ch2=fgetc(fp2);

while(ch1!

=EOF&&ch2!

=EOF){

if(ch1!

=ch2)

break;

elseif(ch1!

='\n'){

j++;

ch1=fgetc(fp1);

ch2=fgetc(fp2);

}

else{

i++;

j=1;

ch1=fgetc(fp1);

ch2=fgetc(fp2);

}

}

printf("首次不同的字符在第%d行、第%d列。

\n",i,j);

if(fclose(fp1)){

printf("cannotclosefile!

\n");

exit(0);

}

if(fclose(fp2)){

printf("cannotclosefile!

\n");

exit(0);

}

}

4.将文件中的数据求和并写入文本文件尾:

文件中存放了若干整数,将文件中所有数据相加,并把累加和写入该文件的最后。

试编写相应程序。

解答:

#include<>

#include<>

intmain(void)

{

FILE*fp;

intx,sum=0;

if((fp=fopen("","ab+"))==NULL){

printf("cannotopenfile!

");

exit(0);

}

while(!

feof(fp)){

fscanf(fp,"%d",&x);

sum+=x;

}

fprintf(fp,"%d",sum);

if(fclose(fp)){

printf("cannotclosethefile!

\n");

exit(0);

}

return0;

}

5.输出含for的行:

将文本文件中所有包含字符串“for”的行输

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2