C语言把txt赋给结构体
typedef struct employee{
int eid;
char name[30];
char gender;
int age;
float score;
char level;
} Emp;
Emp emp[20];
void WriteFile()
{
int i;
FILE *p;
p = fopen("D:\\aaa.txt","w");
for ( i = 0; i < n; i++)
{
fprintf(p, "%d,", emp[i].eid);
fprintf(p, "%s,", emp[i].name);
fprintf(p, "%c,", emp[i].gender);
fprintf(p, "%d,", emp[i].age);
fprintf(p, "%f,", emp[i].score);
}
fclose(p);
}
int ReadFile()
{
FILE *p = fopen("D:\\aaa.txt", "r");
int i;
for (i=0; feof(p); i++)
{
fscanf(p, "%d,", &emp[i].eid);\\
fscanf(p, "%s,", emp[i].name);\\
fscanf(p, "%c,", &emp[i].gender);\\
fscanf(p, "%d,", &emp[i].age);\\
fscanf(p, "%f,", &emp[i].score);\\
if (SetLevel(emp[i].score) == 1)
emp[i].level = 'O';
else if (SetLevel(emp[i].score) == 2)
emp[i].level = 'E';
else if (SetLevel(emp[i].score) == 3)
emp[i].level = 'A';
else if (SetLevel(emp[i].score) == 4)
emp[i].level = 'P';
else
emp[i].level = 'D';
n++;
}
return 0;
}
主程序和其他函数没写 用不来C的文件 求指导 帮别人做作业 他们老师不让用C++ 有问题的都用\\标记了