| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 387 人关注过本帖
标题:关于结构数组的第二贴
只看楼主 加入收藏
学c
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-17
收藏
 问题点数:0 回复次数:0 
关于结构数组的第二贴

这两天一直研究结构数组占内存的问题,同时又发现另一个问题:现在我帖出两段程序请大家分析,一定会有收获的哦!现在我说一下这两个程序:第一个程序是创建一个文件date.txt然后输入学生的学号和分数,然后输出分数大于60的同学。第二个程序是我在计算机中手动建立一个文件叫data.txt,里面包含十个同学的学号和分数,然后利用文件操作去读取,但是输出有一部分是对的,一部分是乱码。我分析是我建立的文本的格式和文件读取操作的格式不匹配,现在请大家分析一下:
(1)#include<stdio.h>
struct student
{
char num[10];
int sco;

} stu_list[10];

main()
{
FILE *fp;
int i,j;
for(i=0;i<=9;i++)
{
printf("please input num: %d\n",i+1);
printf("NO.:");
scanf("%s",stu_list[i].num);
printf("please input sco:\n");
scanf("%d",&stu_list[i].sco);

}


if((fp=fopen("date.txt","w"))==NULL)
{
printf("can not open the file");
exit(0);

}
for(i=0;i<=9;i++)
{
if(fwrite(&stu_list[i],sizeof(struct student),1,fp)!=1)
{
printf("write file error");
}

}
fclose(fp);
fp=fopen("date.txt","r");
for(i=0;i<=9;i++)
{
if((fread(&stu_list[i],sizeof(struct student),1,fp))!=1)
{
printf("read file error");
}
if(stu_list[i].sco>=60)
printf("%s %d\n", stu_list[i].num,stu_list[i].sco);
}
fclose(fp);

}


程序(2)
#include<stdio.h>
struct student
{
char num[10];
int sco;
} stu_list[10];

main()
{

FILE *fp;
int i,j;
fp=fopen("data.txt","r");
for(i=0;i<=9;i++)
{
if(fread(&stu_list[i],sizeof(struct student),1,fp)!=1)
{
printf("read file error");
}
if(stu_list[i].sco>=60)
printf("%s %d\n", stu_list[i].num,stu_list[i].sco);
}
fclose(fp);
}

搜索更多相关主题的帖子: 结构 
2007-09-20 17:30
快速回复:关于结构数组的第二贴
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015445 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved