建立数据文件

van_stephen 2008-08-05 04:46:23
做一个学籍成绩管理系统,如何从键盘输入数据,建立数据文件student.dat呢?
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
19452170 2008-08-05
  • 打赏
  • 举报
回复
up一个
laiwusheng 2008-08-05
  • 打赏
  • 举报
回复
DAT files are data files, usually comma delimited, that contain data in ASCII format. These files are able to be open in a variety of programs including Microsoft Word and Microsoft Notepad.

wangdeqie 2008-08-05
  • 打赏
  • 举报
回复
直接打开student.dat,看到的内容,会比较奇怪,因为文件里存的是二进制的!而输入的是ASC码,转换了一下!
wangdeqie 2008-08-05
  • 打赏
  • 举报
回复

//看这个吧,完整的,先是写入到student.dat,再从student.dat读出!
#include <stdio.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
}stud[SIZE];

void save()
{
FILE* fp;
int i;
if ((fp=fopen("student.dat","wb"))==NULL)
{
printf("can not open ifle\n");
return;
}
for (i=0;i<SIZE;i++)
{
if (fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
{
printf("file write error\n");
}
}
fclose(fp);
}

void load()
{
FILE* fp;
int i;
if ((fp=fopen("student.dat","rb"))==NULL)
{
printf("can not open ifle\n");
return;
}
for (i=0;i<SIZE;i++)
{
fread(&stud[i],sizeof(struct student_type),1,fp);
printf("%-10s %4d %4d\n",stud[i].name,stud[i].num,stud[i].age);
}
fclose(fp);
}
void main()
{
int i;
for (i=0;i<SIZE;i++)
{
scanf("%s%d%d",stud[i].name,&stud[i].num,&stud[i].age);
}
save();
load();



}
//Result under VC6:
li 9 9
zhang 3 4
wang 3 4
liu 4 4
li 9 9
zhang 3 4
wang 3 4
liu 4 4
Press any key to continue

wangdeqie 2008-08-05
  • 打赏
  • 举报
回复

//这么写
#include <stdio.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
}stud[SIZE];

void save()
{
FILE* fp;
int i;
if ((fp=fopen("student.dat","wb"))==NULL)
{
printf("can not open ifle\n");
return;
}
for (i=0;i<SIZE;i++)
{
if (fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
{
printf("file write error\n");
}
}
fclose(fp);
}

void main()
{
int i;
for (i=0;i<SIZE;i++)
{
scanf("%s%d%d",stud[i].name,&stud[i].num,&stud[i].age);
}
save();

}

65,176

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧