Почему, когда я вызываю структуру, она показывает только последний введенный мною ввод - PullRequest
0 голосов
/ 09 апреля 2020

Я пытался создать программу на C для перечисления провинции и ее города, но когда я вызываю структуру, она отображает только последние введенные мной данные.

    #include <stdio.h> 
    int main()
{ 
    FILE *f_structure; 
    int n, i, j, k;
    k=3; 
    struct 
    { 
     char province[30], citya [30], cityb [30], cityc [30]; 
    } list; 
     f_structure = fopen("city List.dat", "wb"); 
     printf("Please input the amount of province: "); 
     scanf("%d", &n); getchar(); 
     for (i=1; i<=n ;i++)
    { 
     printf("Province  : "); 
     gets(list.province);
     printf("city : "); 
     scanf("%s", &list.citya);
     printf("city : "); 
     scanf("%s", &list.cityb);
     printf("city : "); 
     scanf("%s", &list.cityc); 
     getchar(); 
     fwrite(&list, sizeof(list), 1, f_structure); 
    }  
     f_structure = fopen("city List.dat", "rb"); 
     for(j=0;j<=k-1;j++)
     { 
      printf("Province no %d : %s \n city : %s \n city : %s \n city : %s \n",j+1, list.province, list.citya, list.cityb, list.cityc);
     }  
     fclose(f_structure); 
     return 0; 
}
...