C语言fread和fwrite的疑问
#include<stdio.h>#include<stdlib.h>#defineFLIGHT4#defineSEAT12voidread_file(structfli...
#include<stdio.h>#include<stdlib.h>#define FLIGHT 4#define SEAT 12void read_file(struct flight *p, char* filename);void load_data(struct flight *p);void write_file(struct flight *p, char* filename);struct seat{int number;int assign;char lastname[10];char firstname[10];};struct flight{int number;struct seat seats[SEAT];};struct flight flights[4]={{ 102 },{ 311 },{ 444 },{ 519 }};int main(void){int n;read_file(flights, "F://seat.dat");while( 1 ){n = choose_flight();if( n<0 || n>=FLIGHT ) break;while( choose_command( &flights[n] ) ) continue;}write_file(flights, "F://seat.dat");puts("Quit");return 0;}void read_file(struct flight *p, char* filename){FILE * pseats;if ((pseats = fopen(filename, "r")) == NULL){printf("Can't open %s file. So load default data.\n", filename);load_data(p);}else{rewind(pseats); /* go to start of file */while (fread(p, sizeof (struct flight), FLIGHT, pseats) == 1) continue;fclose(pseats);printf("read %s successfully!\n", filename);}}void write_file(struct flight *p, char* filename){FILE * pseats;if ((pseats = fopen(filename, "w")) == NULL){printf("Can't open %s file.", filename);exit(1);}fwrite(p, sizeof (struct seat), SEAT*FLIGHT, pseats);fclose(pseats);}void load_data(struct flight *p){int i,j;for (i=0; i<FLIGHT; i++)for (j=0; j<SEAT; j++){p[i].seats[j].number= i*100 + j ;p[i].seats[j].assign = 0;}}
使用读的时候,数据块大小是结构flight的大小,写的时候却是结构体seat的大小,这里的数据不会丢失么?为什么读出来还是正常显示啊 展开
使用读的时候,数据块大小是结构flight的大小,写的时候却是结构体seat的大小,这里的数据不会丢失么?为什么读出来还是正常显示啊 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询