task1
1 #include<stdio.h> 2 3 #define N 80 4 #define M 100 5 6 7 typedef struct{ 8 char name[N]; 9 char author[N]; 10 }Book; 11 12 void write(); 13 void read(); 14 15 int main(){ 16 printf("测试1:把图书信息写入文本库\n"); 17 write(); 18 19 printf("\n测试2:从文本文件读取图书信息,打印输出到屏幕\n"); 20 read(); 21 22 return 0; 23 24 } 25 26 void write(){ 27 Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 28 {"《灯塔》", "克里斯多夫.夏布特"}, 29 {"《人的局限性》", "塞缪尔.约翰生"}, 30 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 31 {"《大地之上》", "罗欣顿·米斯特里"}, 32 {"《上学记》", "何兆武"}, 33 {"《命运》", "蔡崇达"} }; 34 int n,i; 35 FILE *fp; 36 37 n = sizeof(x)/sizeof(x[0]); 38 39 fp=fopen("datal.txt","w"); 40 41 if(fp==NULL){ 42 printf("fail to open file to write\n"); 43 return ; 44 45 } 46 47 for(i = 0; i < n; ++i) 48 fprintf(fp, "%-40s %-20s\n",x[i].name,x[i].author); 49 50 fclose(fp); 51 } 52 53 void read(){ 54 Book x[M]; 55 int i,n; 56 int number; 57 58 FILE *fp; 59 60 fp=fopen("datal.txt","r"); 61 62 if(fp==NULL){ 63 printf("fail t0 open file to read\n"); 64 return ; 65 } 66 67 i = 0; 68 while(!feof(fp)){ 69 number = fscanf(fp,"%s%s",x[i].name,x[i].author); 70 if(number != 2) 71 break; 72 i++; 73 } 74 n = i; 75 76 for(i = 0 ;i < n; ++i) 77 printf("%d. %-40s%-20s\n", i+1,x[i].name,x[i].author); 78 79 fclose(fp); 80 }
图片
70-71行作用:从不正确输入的地方break,退出循环
task2
1 #include<stdio.h> 2 3 #define N 80 4 #define M 100 5 6 typedef struct{ 7 char name[N]; 8 char author[N]; 9 }Book; 10 11 void write(); 12 void read(); 13 14 int main(){ 15 printf("测试1:把图书信息以数据块方式写入二进制文件\n"); 16 write(); 17 18 printf("\n测试2:从二进制文件读取图书信息,打印输出到屏幕\n"); 19 read(); 20 21 return 0; 22 } 23 24 void write(){ 25 Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 26 {"《灯塔》", "克里斯多夫.夏布特"}, 27 {"《人的局限性》", "塞缪尔.约翰生"}, 28 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 29 {"《大地之上》", "罗欣顿·米斯特里"}, 30 {"《上学记》", "何兆武"}, 31 {"《命运》", "蔡崇达"} }; 32 int n; 33 FILE *fp; 34 35 n = sizeof(x) / sizeof(x[0]); 36 37 fp = fopen("data2.dat","wb"); 38 39 if(fp == NULL){ 40 printf("fail to open file to write\n"); 41 return ; 42 } 43 44 fwrite(x,sizeof(Book),n,fp); 45 46 fclose(fp); 47 } 48 49 void read(){ 50 Book x[M]; 51 int i,n; 52 int number; 53 54 FILE *fp; 55 56 fp = fopen("data2.dat","rb"); 57 58 if(fp == NULL){ 59 printf("fail to open file to read\n"); 60 return ; 61 62 } 63 i = 0; 64 while(!feof(fp)){ 65 number = fread(&x[i],sizeof(Book),1,fp); 66 if (number != 1) 67 break; 68 i++; 69 } 70 71 n = i; 72 for(i = 0; i < n; ++i) 73 printf("%d. %-40s%-20s\n",i+1,x[i].name,x[i].author); 74 75 fclose(fp); 76 77 }
图片
task3
1 #include <stdio.h> 2 #define N 5 3 #define M 80 4 5 void write(); 6 void read_str(); 7 void read_char(); 8 9 int main() { 10 printf("测试1: 把一组字符信息以字符串方式写入文本文件\n"); 11 write(); 12 13 printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n"); 14 read_str(); 15 16 printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n"); 17 read_char(); 18 19 return 0; 20 } 21 22 void write() { 23 char *ptr[N] = { "Working\'s Blues", 24 "Everything Will Flow", 25 "Streets of London", 26 "Perfect Day", 27 "Philadelphia"}; 28 int i; 29 FILE *fp; 30 31 fp = fopen("data3.txt", "w"); 32 if(!fp) { 33 printf("fail to open file to write\n"); 34 return; 35 } 36 37 for(i = 0; i < N; ++i) { 38 fputs(ptr[i], fp); 39 fputs("\n", fp); 40 } 41 42 fclose(fp); 43 } 44 45 void read_str() { 46 char songs[N][M]; 47 int i; 48 FILE *fp; 49 50 fp = fopen("data3.txt", "r"); 51 if(!fp) { 52 printf("fail to open file to read\n"); 53 return; 54 } 55 56 for(i = 0; i < N; ++i) 57 fgets(songs[i], 80, fp); 58 59 for(i = 0; i < N; ++i) 60 printf("%d. %s", i+1, songs[i]); 61 62 fclose(fp); 63 } 64 65 void read_char() { 66 char ch; 67 FILE *fp; 68 69 fp = fopen("data3.txt", "r"); 70 if(!fp) { 71 printf("fail to open file to read\n"); 72 return; 73 } 74 75 while(!feof(fp)) { 76 ch = fgetc(fp); 77 if(ch == EOF) 78 break; 79 80 putchar(ch); 81 } 82 83 fclose(fp); 84 }
图片
task4
1 #include<stdio.h> 2 #define max 256 3 int main(){ 4 FILE *fp; 5 char line[max]; 6 int count_line = 0,count_char = 0; 7 8 fp = fopen("data4.txt","r"); 9 if(fp == NULL){ 10 printf("无法打开文件!\n"); 11 return 0; 12 } 13 14 while(!feof(fp)){ 15 fgets(line,sizeof(line),fp); 16 count_line++; 17 int i; 18 for(i = 0;line[i] != '\0';++i){ 19 if(!isspace(line[i])){ 20 count_char +=1; 21 } 22 } 23 } 24 fclose(fp); 25 printf("\ndata4.txt统计结果:\n"); 26 printf("行数:%d\n",count_line); 27 printf("字符数(不含空白符):%d\n",count_char); 28 29 return 0; 30 }
task5
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; // 准考证号 8 char name[20]; // 姓名 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char result[10]; // 考试结果 13 } STU; 14 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 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, st[i].subjective, st[i].sum, st[i].result); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU s[], int n) { 74 FILE *fp; 75 int i; 76 fp=fopen("examinee.txt","w"); 77 if(fp == NULL){ 78 printf("fail to open file to write\n"); 79 return 0; 80 } 81 for(i = 0; i < n; i++){ 82 if(s[i].sum > 60) 83 fprintf(fp, "%ld%10s%10.2f%10.2f%10.2f%10s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 84 } 85 86 fclose(fp); 87 } 88 89 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 90 int process(STU st[], int n, STU st_pass[]) { 91 int i , count = 0; 92 for(i = 0; i < n; i++){ 93 st[i].sum = st[i].subjective + st[i].objective; 94 if(st[i].sum>=60){ 95 st_pass[count]=st[i]; 96 count++; 97 strcpy(st[i].result,"通过"); 98 } 99 else 100 strcpy(st[i].result,"不通过"); 101 } 102 return count; 103 }
图片
task6
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #define N 80 struct STU{ char num[N]; char name[N]; char classnum[N]; int tmp; }; struct STU st[N],st_choose[N]; void choose(struct STU st[N],struct STU st_choose[N],int n); void output(struct STU st_choose[N]); void write(struct STU t_choose[N]); void read(struct STU st[N],int n); int main(){ int i; for(i = 0; i < N; i++){ st[i].tmp = i+1; read(st,N); choose(st,st_choose,N); output(st_choose); write(st_choose); } return 0; } void read(struct STU st[],int n){ FILE *fp; int i; fp=fopen("list.txt","r"); if(!fp){ printf("error"); return; } while(!feof(fp)){ for(i=1;i<=n;i++) fscanf(fp,"%s %s %s",st[i].num,st[i].name,st[i].classnum); } fclose(fp); } void choose(struct STU st[N],struct STU st_choose[],int n){ int i,j,z,g=1; int x[N]={0}; srand((unsigned)time(NULL));//设随机种子 for(i=1;i<=5;i++) { x[i]=rand()%n+1; for(j=1;j<i;j++) { if(x[i]==x[j]) //保证随机数互不相同 i--; } } for(i=1;i<=n;i++) for(z=1;z<=5;z++) if(st[i].tmp==x[z]) st_choose[g++]=st[i]; } void output(struct STU st_choose[N]) { int i,sum[N]={0},j; struct STU t; printf("--------------随机抽点名单--------------\n"); //升序输出 for(i=1;i<=5;i++) for(j=strlen(st_choose[i].num);j>7;j--) sum[i]+=st_choose[i].num[j]-'0';//将字符学号存入一个数字数组中 for(i=1;i<=4;i++)//冒泡排序 for(j=1;j<=4-i;i++) if(sum[j]>sum[j+1]) { t=st_choose[j]; st_choose[j]=st_choose[j+1]; st_choose[j+1]=t; } for(i=1;i<=5;i++) printf("%s %s %s\n",st_choose[i].num,st_choose[i].name,st_choose[i].classnum); printf("--------------保存到文件------------\n"); } void write(struct STU st_choose[N]){ int i; char str[N]; int info_day,info_month,info_year; time_t rawtime; //声明初始时间 struct tm *info; //调用local函数产生的是结构体 ,定义info指针 time(&rawtime); info=localtime(&rawtime);//调用localtime函数 strftime(str,80,"%Y%m%d",info);//调用strftime函数格式化 strcat(str,".txt"); FILE *fp; fp=fopen(str,"w"); if(!fp) { printf("error"); return; } for(i=1;i<=5;i++) fprintf(fp,"%s %s %s\n",st_choose[i].num,st_choose[i].name,st_choose[i].classnum); fclose(fp); printf("文件保存成功!"); }
图片
标签:fp,int,void,st,read,实验,printf From: https://www.cnblogs.com/YamadaRyo1314/p/18623181