Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
FILE *test; student st; int i = 0; test = fopen("example.bin", "rb"); while (feof(test)) { fread(&st, sizeof(st),1,test); Class[i] = st; i++; } fclose(test);
所以我的问题是如何阅读它,将数据放入我的结构中并停止循环?
看看为什么 while(feof) 总是错误的。
相反,您应该循环直到您无法阅读学生:
while (1 == fread(&st, sizeof st, 1, test)) { Class[i++] = st; }
此外,最好确保您不会溢出 的缓冲区Class,因此也要检查一下i,例如:
Class
i
for (i = 0; i < MAX_CLASS && 1 == fread(&st, sizeof st, 1, test); ++i) { Class[i] = st; }