scjylxs scjylxs
关注数: 17 粉丝数: 38 发帖数: 2,440 关注贴吧数: 15
为什么一运行这个读取并显示内容的程序就提示程序错误? 请各位朋友帮忙看一看,这是什么原因造成的,一直没有解决这个问题。 平台:CFREE 生成文件的程序代码如下: #include<stdio.h> #include"stdlib.h" #define SIZE 4 structstudent_type { char name[10]; intnum; int age; char addr[15]; }stud[SIZE]; void save() { FILE*fp; int i; if((fp=fopen("c:/lx/stu_list.dat","wb"))==NULL) { printf("不能打开文件!"); return; } for(i=0;i<SIZE;i++) { if(fwrite(&stud[i],sizeof(structstudent_type),1,fp)!=1) printf("写文件错误!\n"); } fclose(fp); } int main() { inti; printf("输入学生信息:姓名,学号,房间号\n"); for(i=0;i<SIZE;i++) scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr); save(); //调用保存到文件的函数 } 输入数据: 张林 1001 19 room-101 勾文月1002 20 room-102 唐文清1003 21 room-103 宁静 1004 21 room-104 读取的文件保存在C:\lx\stud_list.dat 下面是读取上面所生成文件的程序,程序功能:读取文件并显示(不能正确运行,一运行就提示程序错误是否中断或调试) 程序代码如下: #include "stdio.h" #include "stdlib.h" #define SIZE 4 struct student_type { char name[10]; int num; int age; char addr[15]; }stud[SIZE]; int main() { int i; FILE *fp; fp=fopen("c:/lx/stu_list.dat","rb"); if(fp==NULL) { printf("不能打开文件!"); exit(0); } for(i=0;i<SIZE;i++) { fread(&stud[i],sizeof(struct student_type),1,fp); printf("%-10s%4d%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr); } fclose(fp); }
1 下一页