Reputation: 29
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);
}
}
}
}
Ok, so I'm kinda lost right now. Case 2 where I add strings to text file works flawlessly, but when when I want to check out the added data to the text file using the case 1, program crashes, so far Ive tried several things and no improvements. Would be very thankful for some help!
Upvotes: 0
Views: 90
Reputation: 13
In case 1:
you are fclose
-ing every time you loop which means that after the first loop it will close the file then when it tries to feof
and fscanf
in the next loop the program will fail.
Upvotes: 1