资源描述:
《C语言程序设计 教学课件 作者 罗朝盛第9章 习题参考答案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、《C语言程序设计》习题参考答案第9章习题参考答案1.填空题(1)文本文件/二进制文件(2)创建a文件(3)打开a文件,并且文件读写位置指针在文件尾(4)将字符变量ch中的字符写入fp所指向的文件/从fp所指向的文件中读取n-1个字符,并且写入数组s中(5)0(6)for(i=0;i<30;i++)fread(&stud[i],sizeof(structstudent_type),1,fp)(7)fseek(fp,30L,SEEK_SET)(8)-12.选择题12345678ABCDCCDC3.程序阅读题1-
2、5,-4,-3,-2,total=-142Language!3123456789104.程序填空题1(fp=fopen("c:\data.txt","w"))(ch=getchar())fputc(ch,fp)2stu[i].name,stu[i].sno,stu[i].age,stu[i].address"w"&stu[i],sizeof(structstudent),1,fp《C语言程序设计》习题参考答案3rw5.编程题(1)#includevoidmain(){FILE*fp1,*
3、fp2;charch;if((fp1=fopen("C:\Mydir\String.txt","r"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}if((fp2=fopen("C:\Mydir\NewString.txt","w"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}while((ch=fgetc(fp1))!=EOF){if(ch>='A'
4、&&ch<='Z')ch+=32;elseif(ch>='a'&&ch<='z')ch-=32;fputc(ch,fp2);};fclose(fp1);fclose(fp2);remove("C:\Mydir\String.txt");rename("C:\Mydir\String.txt","C:\Mydir\NewString.txt");}(2)#includeintsearch(FILE*fp,charch);voidmain(){FILE*fp;charfilenam
5、e[30];charch;intcount;《C语言程序设计》习题参考答案printf("Pleseinputfilename:");gets(filename);printf("Pleseinputthecharacteryouwanttosearch:");scanf("%c",&ch);if((fp=fopen(filename,"r"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}count=search(fp,ch)
6、;fclose(fp);printf("%dtimes!",count);}intsearch(FILE*fp,charch){intnum=0;charx;while((x=fgetc(fp))!=EOF)if(x==ch)num++;returnnum;}(3)#includevoidmain(){FILE*fp1,*fp2;intx,temp;printf("Pleaseinputthenumber:");scanf("%d",&x);if((fp1=fopen("C:\M
7、ydir\source.dat","r"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}if((fp2=fopen("C:\Mydir\destination.dat","w"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");exit(0);}while(!feof(fp1))《C语言程序设计》习题参考答案{fscanf(fp1,"%d",&temp);if(temp%x==
8、0)fprintf(fp2,"%d",temp);};fclose(fp1);fclose(fp2);}(4)#include#defineSTUNUM5/*代表学生人数*/structstudent{charname[20];intChinese;intComputer;intMath;}stu1[STUNUM],stu2[STUNUM];voidmain(){FILE*fp1,*fp2;in