本帖最后由 Abuzzworld 于 2015-10-7 13:50 编辑
- #include <stdio.h>
- typedef struct {
- int year;
- int month;
- int day;
- }DATE;
- struct Student {
- char name[30];
- int height;
- int weitht;
- DATE date;
- }stu[4];
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- //接收输入数据到结构体中
- for (int i=0; i<2; i++) {
- scanf("%s%d%d%d%d%d",stu[i].name,&stu[i].height,&stu[i].weitht,&stu[i].date.year,&stu[i].date.month,&stu[i].date.day);
- }
-
- FILE *fp=fopen("/Users/Zxx/Desktop/test/student.data","wb+");
-
- //将结构体写入文件
- if (fp!=NULL) {
- for (int i=0; i<0; i++) {
- fwrite(&stu[i], sizeof(struct Student), 1, fp);
- }
- }else{
- printf("文件打开失败,按任意键退出\n");
- getchar();
- exit(1);
- }
- rewind(fp);
- //读取文件数据到结构体中
- for (int i=2; i<4; i++) {
- fread(&stu[i], sizeof(struct Student), 1, fp);
- printf("%s %d %d %d %02d %02d\n",stu[i].name,stu[i].height,stu[i].weitht,stu[i].date.year,stu[i].date.month,stu[i].date.day);
- }
- fclose(fp);
- }
- return 0;
- }
复制代码 student.data一直是0kb,到底是哪里出错了啊 |
|