int main() {
scanf("%d",&a);
while(1) {
switch(a) {
case 0:
exit(0);
case 1:
fp = fopen("info.txt", "r");
if (fp == NULL)
printf("File doest not exist! \n");
else {
for(i=0; !feof(fp); i++) {
fscanf(fp, "%s %s %s %s %s %s",
user[i].name,
user[i].surname,
user[i].gender,
user[i].status,
user[i].education,
user[i].adress);
printf("%s, %s, %s, %s, %s, %s\n",
user[i].name,
user[i].surname,
user[i].gender,
user[i].status,
user[i].education,
user[i].adress);
fclose(fp);
}
}
break;
case 2:
for(i=0; i<N; i++) {
printf("\nUser application data\n", i+1);
printf("Name: ");
gets(user[i].name);
gets(user[i].name);
printf("Surname: ");
gets(user[i].surname);
printf("Gender: ");
gets(user[i].gender);
printf("Status: ");
gets(user[i].status);
printf("Education: ");
gets(user[i].education);
printf("Adress: ");
gets(user[i].adress);
fp = fopen("info.txt", "a");
fprintf(fp, "\n%s %s %s %s %s %s",
user[i].name,
user[i].surname,
user[i].gender,
user[i].status,
user[i].education,
user[i].adress);
fclose(fp);
}
}
}
}
好的,所以我现在有点失落。案例2我在文本文件中添加字符串完美无缺,但是当我想使用案例1查看添加到文本文件中的数据时,程序崩溃了,到目前为止我已经尝试了几件事而没有改进。非常感谢一些帮助!
答案 0 :(得分:1)
在case 1:
中,您fclose
- 每次循环时都意味着在第一次循环后它将关闭文件,然后在尝试feof
和fscanf
时在下一个循环中,程序将失败。