c - 为什么当我调用结构时它只显示我给出的最后一个输入
问题描述
我试图用 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;
}
解决方案
推荐阅读
- dart - angulardart中的两种方式绑定和监听器
- java - 获取 HashMap 中所有值的大小
> - c - C - execvp 不使用有效输入
- html - How to position an element relative to a specific background image object?
- css - 背景颜色超过背景图像 - 原因是什么?
- google-bigquery - Google 数据洞察:日期范围过滤器错误
- c++ - 在 C++ 中生成具有给定概率的随机数
- gcc - 如何修复符号 __vdso_clock_gettime,版本 GLIBC_PRIVATE 未在文件 libc.so.6 中定义,并带有链接时间参考
- mysql - 关于对删除重复项的 Sql 查询
- javascript - Catch set 'X-Frame-Options' to 'sameorigin error in js