关于结构体存储到文件中并输出
初学者不是很懂,我在做学生管理系统,但是我的疑问是如果我将值赋到结构体里为何还要赋值到文件里呢?如果赋值到文件又该怎么赋值并输出呢?附上我的程序,请大神指教。
#include<stdio.h>
#include<stdlib.h>
struct info
{
float a;
char b[40];
char c[40];
char d[40];
float e;
float f;
float g;
float h;
float i;
float j;
};
struct info info[10000];
int record(int i){
printf("input your id\n");
scanf("%f", &info[i].a);
printf("input your firstname\n");
scanf("%s", &info[i].b);
getchar();
printf("input your lastname\n");
scanf("%s", &info[i].c);
getchar();
printf("input your e-mail\n");
scanf("%s", &info[i].d);
getchar();
printf("input your a\n");
scanf("%f", &info[i].e);
printf("input your b\n");
scanf("%f", &info[i].f);
printf("input your c\n");
scanf("%f", &info[i].g);
printf("input your d\n");
scanf("%f", &info[i].h);
printf("input your e\n");
scanf("%f", &info[i].i);
printf("input your f\n");
scanf("%f", &info[i].j);
FILE*fp;
if ((fp = fopen("1.text", "wb")) == NULL){
printf("wrong");
}
fwrite(&info[i], sizeof(info[i]), i, fp);
}
int find(int id){
int i = 0;
for (i = 0; i < 1000; i++)
{
if (id == info[i].a){
return i;
break;
}
}
return -1;
}
int mark(int i){
return(info[i].e + info[i].f + info[i].g + info[i].h + info[i].i + info[i].j) / 6;
}
display(int z){
FILE*fp;
if ((fp = fopen("1.text", "r+")) == NULL){
printf("wrong");
}
fprintf(fp,"%s",info[z].b);
}
int main(){
int choose;
int j = 0;
FILE*fp;
while (1){
printf("choose\n");
scanf("%d", &choose);
switch (choose)
{
case 1:j = j + 1; record(j);break;
case 2:printf("please enter your id\n"); int ID; scanf("%d", &ID); int z = find(ID); int y = mark(z); printf("%d\n", y); display(z); break;
default:break;
}
}
}