实验7
task1
1 #include<stdio.h> 2 #define N 80 3 #define M 100 4 typedef struct{ 5 char name[N]; 6 char author[N]; 7 }book; 8 void write(); 9 void read(); 10 int main(){ 11 printf("测试1: 把图书信息写入文本文件\n"); 12 write(); 13 printf("\n测试2: 从文本文件读取图书信息, 打印输出到屏幕\n"); 14 read(); 15 return 0; 16 } 17 void write(){ 18 book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 19 {"《灯塔》", "克里斯多夫.夏布特"}, 20 {"《人的局限性》", "塞缪尔.约翰生"}, 21 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 22 {"《大地之上》", "罗欣顿·米斯特里"}, 23 {"《上学记》", "何兆武"}, 24 {"《命运》", "蔡崇达"} }; 25 int n,i; 26 FILE *fp; 27 n=sizeof(x)/sizeof(x[0]); 28 fp=fopen("data1.txt","w"); 29 if(fp==NULL){ 30 printf("fail to open file to write\n"); 31 return; 32 } 33 for(i=0;i<n;++i){ 34 fprintf(fp,"%-40s %-20s\n",x[i].name ,x[i].author ); 35 } 36 fclose(fp); 37 } 38 void read(){ 39 book x[M]; 40 int i,n; 41 FILE *fp; 42 fp=fopen("data1.txt","r"); 43 if(fp==NULL){ 44 printf("fail to open file to read\n"); 45 return; 46 } 47 i=0; 48 while(fscanf(fp,"%s%s",x[i].name,x[i].author )!=EOF){ 49 ++i; 50 } 51 n=i; 52 for(i = 0; i < n; ++i){ 53 printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); 54 } 55 fclose(fp); 56 }


task2
1 #include<stdio.h> 2 #define N 80 3 #define M 100 4 typedef struct{ 5 char name[N]; 6 char author[N]; 7 }book; 8 void write(); 9 void read(); 10 int main(){ 11 printf("测试1: 把图书信息以数据块方式写入二进制文件\n"); 12 write(); 13 printf("\n测试2: 从二进制文件读取图书信息, 打印输出到屏幕\n"); 14 read(); 15 return 0; 16 } 17 void write(){ 18 book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 19 {"《灯塔》", "克里斯多夫.夏布特"}, 20 {"《人的局限性》", "塞缪尔.约翰生"}, 21 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 22 {"《大地之上》", "罗欣顿·米斯特里"}, 23 {"《上学记》", "何兆武"}, 24 {"《命运》", "蔡崇达"} }; 25 int n,i; 26 FILE *fp; 27 n=sizeof(x)/sizeof(x[0]); 28 fp=fopen("data2.dat","wb"); 29 if(fp==NULL){ 30 printf("fail to open to write\n"); 31 return; 32 } 33 fwrite(x,sizeof(book),n,fp); 34 fclose(fp); 35 } 36 void read(){ 37 book x[M]; 38 int n,i; 39 FILE *fp; 40 fp=fopen("data2.dat","rb"); 41 if(fp==NULL){ 42 printf("fail to open to read\n"); 43 return; 44 } 45 i=0; 46 while(fread(&x[i],sizeof(book),1,fp)==1){ 47 ++i; 48 } 49 n=i; 50 for(i = 0; i < n; ++i) 51 printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); 52 fclose(fp); 53 }

task3
1 #include<stdio.h> 2 #define N 100 3 #define M 80 4 void write(); 5 void readchar(); 6 void readstr(); 7 int main(){ 8 printf("测试1: 把一组字符信息以字符串方式写入文本文件\n"); 9 write(); 10 printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n"); 11 readstr(); 12 printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n"); 13 readchar(); 14 return 0; 15 } 16 void write(){ 17 char *ptr[] = { "Working\'s Blues", 18 "Everything Will Flow", 19 "Streets of London", 20 "Perfect Day", 21 "Philadelphia"}; 22 int n,i; 23 FILE *fp; 24 fp=fopen("data3.txt","w"); 25 if(fp == NULL) { 26 printf("fail to open file to write\n"); 27 return; 28 } 29 n = sizeof(ptr)/sizeof(ptr[0]); 30 for(i=0;i<n;++i){ 31 fputs(ptr[i],fp); 32 fputs("\n",fp); 33 } 34 fclose(fp); 35 } 36 void readstr(){ 37 char songs[N][M]; 38 int i, n; 39 FILE *fp; 40 fp = fopen("data3.txt", "r"); 41 if(fp == NULL) { 42 printf("fail to open file to read\n"); 43 return; 44 } 45 i = 0; 46 while(i<N&&(fgets(songs[i],M,fp)!=NULL)){ 47 ++i; 48 } 49 n=i; 50 for(i=0;i<n;++i){ 51 printf("%d. %s",i+1,songs[i]); 52 } 53 fclose(fp); 54 } 55 void readchar(){ 56 int ch; 57 FILE *fp; 58 fp=fopen("data3.txt","r"); 59 if(fp==NULL){ 60 printf("fail to open file to read\n"); 61 return; 62 } 63 while((ch=fgetc(fp))!=EOF){ 64 putchar(ch); 65 } 66 fclose(fp); 67 }


task4
1 #include<stdio.h> 2 int main(){ 3 FILE *fp; 4 int i,n,ch,m=0; 5 fp=fopen("data4.txt","r"); 6 if(fp==NULL){ 7 printf("fail to open file to read\n"); 8 return; 9 } 10 i=0; 11 while((ch=fgetc(fp))!=EOF){ 12 if(ch!=' '&&ch!='\n'&&ch!='\t'){ 13 i++; 14 } 15 if(ch=='\n'){ 16 m++; 17 } 18 } 19 m++; 20 n=i; 21 fclose(fp); 22 printf("data4.txt统计结果:\n"); 23 printf("行数:%d\n",m); 24 printf("字符数(不记空白):%d\n",n); 25 return 0; 26 }

\为转义字符,\‘表示字符’
防止读取的内容超过数组song的容量
task5
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 10 5 typedef struct{ 6 long id; 7 char name[20]; 8 float objective; 9 float subjective; 10 float sum; 11 char result[10]; 12 }stu; 13 void read(stu st[],int n); 14 void write(stu st[],int n); 15 void output(stu st[],int n); 16 int process(stu st[],int n,stu stpass[]); 17 int main(){ 18 stu st[N],stpass[N]; 19 int cnt; 20 double passrate; 21 printf("从文件读入%d个考生信息...\n", N); 22 read(st,N); 23 printf("\n对考生成绩进行统计...\n"); 24 cnt =process(st,N,stpass); 25 printf("\n通过考试的名单:\n"); 26 output(st,cnt); 27 write(st,N); 28 passrate=1.0*cnt/N; 29 printf("\n本次等级考试通过率: %.2f%%\n", passrate*100); 30 return 0; 31 } 32 void read(stu st[],int n){ 33 int i; 34 FILE *fin; 35 fin=fopen("examinee.txt","r"); 36 if (!fin) { 37 printf("fail to open file\n"); 38 return; 39 } 40 for(i=0;i<n;++i){ 41 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, 42 &st[i].subjective); 43 } 44 fclose(fin); 45 } 46 void output(stu st[],int n){ 47 int i=0; 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for(i;i<n;++i){ 50 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name,st[i].objective, 51 st[i].subjective, st[i].sum, st[i].result); 52 } 53 } 54 void write(stu st[],int n){ 55 FILE *fout; 56 int i; 57 fout=fopen("list_past.txt","w"); 58 if(!fout){ 59 printf("fail to open to write\n"); 60 return; 61 } 62 for(i=0;i<n;++i){ 63 fprintf(fout,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",st[i].id,st[i].name, 64 st[i].objective,st[i].subjective,st[i].sum,st[i].result); 65 } 66 fclose(fout); 67 } 68 int process(stu st[],int n,stu stpass[]){ 69 int cnt=0,i; 70 for(i=0;i<n;++i){ 71 st[i].sum=st[i].objective+st[i].subjective; 72 if(st[i].sum>=60){ 73 strcpy(st[i].result,"通过"); 74 stpass[cnt++]=st[i]; 75 } 76 else 77 strcpy(st[i].result,"未通过"); 78 } 79 return cnt; 80 }


task6
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<time.h> 5 #define N 100 6 #define m 5 7 int main(){ 8 char s[N][N],t[N][N],name[20]; 9 int i=0,n,p; 10 int hashit[N]={0}; 11 srand(time(0)); 12 FILE *fp1,*fp2; 13 fp1=fopen("list.txt","r"); 14 if(!fp1){ 15 ferror(fp1); 16 return 1; 17 } 18 while(fgets(s[i],N,fp1)!=NULL){ 19 i++; 20 } 21 n=i; 22 fclose(fp1); 23 for(i=0;i<n;++i){ 24 printf("%s",s[i]); 25 } 26 printf("-------------中奖名单-------------\n"); 27 for(i=0;i<m;++i){ 28 p=rand()%n; 29 if(hashit[p]) 30 continue; 31 hashit[p]=1; 32 printf("%s",s[p]); 33 strcpy(t[i],s[p]); 34 } 35 printf("------------保存到文件-------------\n"); 36 printf("输入文件名:"); 37 scanf("%s",name); 38 fp2=fopen(name,"w"); 39 if(!fp2){ 40 ferror(fp2); 41 return 1; 42 } 43 for(i=0;i<m;++i){ 44 fprintf(fp2,"%s\n",t[i]); 45 } 46 fclose(fp2); 47 printf("保存成功!\n"); 48 return 0; 49 }




浙公网安备 33010602011771号