C程序第9章习题参考答案
- 格式:doc
- 大小:72.50 KB
- 文档页数:10
习题9参考答案 一、单项选择题 ADBBC CCCDC 二、填空题 ① 显示器 ② w ③ 指定文件内部记录指针移动的起始位置 ④ sizeof(student) 三、阅读程序题 1. younk 2. 文件拷贝 3. 1 3 5 7 9 4. Name: Lilei Phone:123456 Score:100 5. AAA 6. 18
四、程序设计题 //xt090401.cpp #include #include void main() { struct person{ char name[20]; char sex; int age; float height; }person1; FILE *fp; char fname[20]; int i; printf("please input a file name:\n"); scanf("%s",fname); if((fp=fopen(fname,"wb"))==NULL) { printf("cannot open this file!\n"); exit(1); } printf("please input three persons' name,sex,age and height:\n"); for(i=0;i<3;i++) { scanf("%s %c%d%f",person1.name,&person1.sex,&person1.age,&person1.height); if(fwrite(&person1,sizeof(person1),1,fp)!=1) { printf("write error!\n"); exit(2); } } fclose(fp); if((fp=fopen(fname,"rb"))==NULL) { printf("cannot open this file!\n"); exit(3); } printf("\nthe content of the file is:\nName\t\tSex\tAge\tHeight\n"); while(!feof(fp)) { if(fread(&person1,sizeof(person1),1,fp)==1) printf("%-16s%-8c%-8d%-10.2f\n",person1.name,person1.sex, person1.age,person1.height); } fclose(fp); exit(0); } //xt090402.cpp #include #include #include void main() { int i; FILE *fp; if((fp=fopen("result.txt","w"))==NULL) { puts("OPen file failed!"); exit(1); } double result=0.0; for(i=0;i<=32;i++) { result=cos((double)(i*2)/32); printf("%lf\n",result); fprintf(fp,"%9.6f",result); } fclose(fp); } //xt090403.cpp #include #include void main() { FILE *fp; int i,j,n,ni; char c[160],t,ch; if((fp=fopen("file1","r"))==NULL) { printf("file 1 cannot be opened\n"); exit(1); } printf("\n A contents are :\n"); for(i=0;(ch=fgetc(fp))!=EOF;i++) { c[i]=ch; putchar(c[i]); } fclose(fp); ni=i; if((fp=fopen("file2","r"))==NULL) { printf("file 2 cannot be opened\n"); exit(2); } printf("\n B contents are :\n"); for(;(ch=fgetc(fp))!=EOF;i++) { c[i]=ch; putchar(c[i]); } fclose(fp); n=i; for(i=0;ifor(j=i+1;jif(c[i]>c[j]) { t=c[i];c[i]=c[j];c[j]=t; } if((fp=fopen("file3","w"))==NULL) { printf("file 3 cannot be opened\n"); exit(3); } for(i=0;i{ putc(c[i],fp); putchar(c[i]); } fclose(fp); exit(0); } //xt090404.cpp #include #include #include void main() { FILE *fp; char str[100]; int i=0; if((fp=fopen("test","w"))==NULL) { printf("cannot open the file\n"); exit(1); } printf("please input a string:\n"); gets(str); while(str[i]!='\0') { if(str[i]>='a'&&str[i]<='z') str[i]=str[i]-32; fputc(str[i],fp); i++; } fclose(fp); fp=fopen("test","r"); fgets(str,strlen(str)+1,fp); printf("%s\n",str); fclose(fp); } //xt090405.cpp #include int main(int argc, char* argv[]) { char filename[20]; FILE *fpt; printf("Input filename:\n"); gets(filename); if((fpt=fopen(filename,"w"))==NULL) { printf("Can not Open File!"); return 1; } char ch; ch=getchar(); while(ch!='#') { fputc(ch,fpt); ch=getchar(); } return 0; } //xt090406.cpp #include char Line[2000]; void clearLine() { Line[0]='\0'; } int getMaxLineNum(char filename[20]) { int LineMax=0,Count=0,LineNum=0,LenMaxLine=0; char ch; FILE *fpt; if((fpt=fopen(filename,"r"))==NULL) { printf("Can not Open File!"); return 1; } ch=fgetc(fpt); while(ch!=EOF) { putchar(ch); if(ch==10) { if(LineMax{ LineMax=Count; LenMaxLine=LineNum; } Count=0; LineNum++; } else Count++; ch=fgetc(fpt); } fclose(fpt); return LenMaxLine; } int printline(char filename[],int line) { int LineNum=0; char ch; FILE *fpt; if((fpt=fopen(filename,"r"))==NULL) { printf("Can not Open File!"); return 1; } ch=fgetc(fpt); while(ch!=EOF) { if(LineNum{ ch=fgetc(fpt); if(ch==10) LineNum++; } if(LineNum==line) { ch=fgetc(fpt); putchar(ch); if(ch==10)