|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<math.h>
//数据块读写
//从键盘输入4个学生的有关数据,然后把他们以二进制的格式存储到磁盘文件中
#define SIZE 2
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save();
int main()
{
int i;
printf("输入学生的内容");
for(i=0;i<SIZE;i++)
{
scanf("%s %d %d %s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
}
save();
}
void save()
{
FILE *fp;
int i;
if(!(fp=open("student-list","wb")))
{
printf("没有这个文件");
return;
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1) // 单点调试在这个位置报错, 和源码没什么区别, 求问原因。 一头雾水
{
printf("文件写入错误");
}
fclose(fp);
}
}
- #include<stdio.h>
- #include<math.h>
- //数据块读写
- //从键盘输入4个学生的有关数据,然后把他们以二进制的格式存储到磁盘文件中
- #define SIZE 2
- struct student_type
- {
- char name[10];
- int num;
- int age;
- char addr[15];
- }stud[SIZE];
- void save();
- int main()
- {
- int i;
- printf("输入学生的内容");
- for(i=0;i<SIZE;i++)
- {
- scanf("%s %d %d %s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
- }
- save();
- return 0;
- }
- void save()
- {
- FILE *fp;
- int i;
- //if(!(fp=open("student-list","wb")))
- if(!(fp=fopen("student-list","wb")))
- {
- printf("没有这个文件");
- return;
- }
- for(i=0;i<SIZE;i++)
- {
- if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1) // 单点调试在这个位置报错, 和源码没什么区别, 求问原因。 一头雾水
- {
- printf("文件写入错误");
- }
- //fclose(fp);
- }
- fclose(fp);
- }
复制代码
|
|