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!

8
  • 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. Commented Jan 9, 2014 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. Commented Jan 9, 2014 at 22:08
  • We need the definition of struct User Commented Jan 9, 2014 at 22:09
  • I think you may need to revisit your for loop statement Commented Jan 9, 2014 at 22:11
  • FILE *fp; struct info { int Nr; char name[25], surname[25], gender[15], status[25], education[25], adress[25]; } user[N]; Commented Jan 9, 2014 at 22:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.