[文件]学生信息的简单读入与输出


fscanf和fprintf搭配


1
#include<stdio.h> 2 #include<malloc.h> 3 #include<stdlib.h> 4 typedef struct student{ 5 char name[10]; 6 char num[11]; 7 int age; 8 float score; 9 }stu; 10 int main() 11 { 12 int i,N;//学生人数 13 printf("Input the number of students\n"); 14 scanf("%d",&N); 15 stu *write,*read,*pa,*pb;//分别用来输入和读取 16 write = (stu*)malloc(sizeof(stu)*N); 17 read = (stu*)malloc(sizeof(stu)*N); 18 pa = write; 19 pb = read; 20 21 FILE* fp=fopen("D:\\student.txt","wb+");//文件指针 22 if(fp == NULL){ 23 printf("Fail to open file!\n"); 24 exit(0); 25 } 26 27 printf("Input data:\n"); 28 for(i=0; i < N; i ++,pa ++){//从键盘读入数据,保存到write 29 printf("name:"); 30 scanf("%s",pa->name); 31 printf("student number:"); 32 scanf("%s",pa->num); 33 printf("age:"); 34 scanf("%d",&pa->age); 35 printf("score:"); 36 scanf("%f",&pa->score); 37 } 38 pa = write; 39 //将write中的数据写入到文件 40 for(i=0; i<N; i++,pa++){ 41 fprintf(fp,"%s %s %d %f\n", pa->name, pa->num, pa->age, pa->score); 42 } 43 //重置文件指针 44 rewind(fp); 45 //从文件中读取数据,保存到read 46 for(i=0; i<N; i++,pb++){ 47 fscanf(fp, "%s %s %d %f\n", pb->name, &pb->num, &pb->age, &pb->score); 48 } 49 pb = read; 50 //将read中的数据输出到显示器 51 printf("name\tnumber\tage\tscore\n"); 52 for(int i=0; i < N; i ++,pb ++){ 53 printf("[%s]\t",pb->name); 54 printf("%s\t",pb->num); 55 printf("%d\t",pb->age); 56 printf("%.1f\n",pb->score); 57 } 58 fclose(fp); 59 return 0; 60 }

用 fprintf() 和 fscanf() 函数读写配置文件、日志文件会非常方便,不但程序能够识别,用户也可以看懂,可以手动修改。

 

注: 

printf("Input the number of students\n");
scanf("%d",&N);
当我们输入人数,按下空格后D盘才多出来了一个"student.txt"文件

 

 

 

 

 


fread和fwrite搭配


1
#include<stdio.h> 2 #include<malloc.h> 3 #include<stdlib.h> 4 typedef struct student{ 5 char name[10]; 6 char num[11]; 7 int age; 8 float score; 9 }stu; 10 int main() 11 { 12 int i,N;//学生人数 13 printf("Input the number of students\n"); 14 scanf("%d",&N); 15 stu *write,*read,*pa,*pb;//分别用来输入和读取 16 write=(stu*)malloc(sizeof(stu)*N); 17 read=(stu*)malloc(sizeof(stu)*N); 18 pa = write; 19 pb = read; 20 21 FILE* fp=fopen("D:\\stu.txt","wb+");//文件指针 22 if(fp == NULL){ 23 printf("Fail to open file!\n"); 24 exit(0); 25 } 26 27 printf("Input data:\n");//写入数据 28 for(i=0; i < N; i ++,pa ++){ 29 printf("name:"); 30 scanf("%s",pa->name); 31 printf("student number:"); 32 scanf("%s",pa->num); 33 printf("age:"); 34 scanf("%d",&pa->age); 35 printf("score:"); 36 scanf("%f",&pa->score); 37 } 38 fwrite(write,sizeof(stu),N,fp);//将write数组写入文件 39 rewind(fp);//文件指针重置 40 fread(read,sizeof(stu),N,fp);//输出read数组输出 41 42 printf("name\tnumber\tage\tscore\n"); 43 for(i=0; i < N; i ++,pb ++){ 44 printf("[%s]\t",pb->name); 45 printf("%s\t",pb->num); 46 printf("%d\t",pb->age); 47 printf("%.1f\n",pb->score); 48 } 49 fclose(fp); 50 return 0; 51 }

 

我们也可以用fwrite和fread,但会发现"student.txt"中出现类似如下的令人摸不到头绪的数据,这是为什么呢?

fread和 fwrite 必须使用 2进制操作方式,否则操作的字节数【可能】不正确。 

原因是 2进制操作时候, 
操作不考虑字符意义, 
纯粹操作字节流。 
也就是操作的的数据按字节 “流”向 文件(或者读取出来), 
但是这个字节的意义不予考虑, 
所以参数指定多少长度,就读写多少字节。 

但是文本方式不同, 
这种操作方式下,考虑字符意义,而不是纯粹考虑字节流。 
一个常见的例子,\n 在win下,文本方式操作, 
写文件的时候,根据字符意义,会在文件中写入两个字符 \r\n 
而文件中的 \r\n 在文本方式读取的时候, 会根据字符意义被转换成\n一个字符【如果是2进制读取的话,这里读取的就是两个字符】

posted @ 2021-01-05 23:19  泥烟  阅读(234)  评论(0编辑  收藏  举报