0
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!

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
Marty
  • 29
  • 5
  • 3
    Your code was a total mess, this time I formatted it for you but for the future: please be kind towards other users and always provide nice, readable code. – Stefano Sanfilippo Jan 09 '14 at 22:07
  • @StefanoSanfilippo yeah his code was really a total mess. I'm glad you sorted it out half way through deciphering his code. Thanks. – Pavan Jan 09 '14 at 22:08
  • We need the definition of `struct User` – Stefano Sanfilippo Jan 09 '14 at 22:09
  • I think you may need to revisit your for loop statement – Pavan Jan 09 '14 at 22:11
  • FILE *fp; struct info { int Nr; char name[25], surname[25], gender[15], status[25], education[25], adress[25]; } user[N]; – Marty Jan 09 '14 at 22:12
  • You need to read [Why while (!feof(file)) is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong); you are suffering from that, which can lead to accessing out of bounds memory. You should also check the results of your scanf() statements. You should not be using `gets()`; that is a recipe for disaster (it isn't even a standard C function any more). Please update your question with the structure definition. – Jonathan Leffler Jan 09 '14 at 22:12
  • Avoid using `scanf("%d",&a);` and _then_ `gets()`. Better yet, use `fgets()` rather than `gets()`, but still avoid mixing `fgets()` with `scanf()`. Best to use `fgets()` to get user input and use `sscanf()` to parse as needed. – chux - Reinstate Monica Jan 09 '14 at 22:20
  • Finally working, thanks y'all! Much appreciated! – Marty Jan 09 '14 at 22:21

1 Answers1

1

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.

qrpnxz
  • 13
  • 5