| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1533 人关注过本帖
标题:[求助]关于fread读数据时的问题
只看楼主 加入收藏
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
 问题点数:0 回复次数:10 
[求助]关于fread读数据时的问题

我们的课程设计要求用十字链表结构
建立十字链表没有问题
然后我用fwrite存好也没错
接着用fread读的时候问题来了
当我把第一层结点定义如下
struct col
{
unsigned long colnum;
char colname[10];
struct col *next;
struct cour *first;
unsigned m; /*下一层结点计数*/
};
m的值怎么也读不出来,总是0
当我把第一层结点定义如下
struct col
{
unsigned m; /*下一层结点计数*/
unsigned long colnum;
char colname[10];
struct col *next;
struct cour *first;
};
m的值就能读出来了

这是为什么

这个错误我调了两天,诚心向高手求教

搜索更多相关主题的帖子: fread 结点 col 数据 unsigned 
2007-07-23 11:46
anlogo
Rank: 2
等 级:论坛游民
威 望:1
帖 子:293
专家分:20
注 册:2007-7-20
收藏
得分:0 

把存储和读取的程序贴出来看看~~

2007-07-23 12:28
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 

#define COLSIZE (sizeof(struct col)-sizeof(struct col *)-sizeof(struct cour *))
#define COUSRSIZE (sizeof(struct cour)-sizeof(struct cour *)-sizeof(struct res *))
#define RESSIZE (sizeof(struct res)-sizeof(struct res *))
void save_file(struct col *head)
{
struct col *p1;
struct cour *p2;
struct res *p3;
FILE *fp1, *fp2, *fp3;
if ((fp1 = fopen("d:\\college.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
if ((fp2 = fopen("d:\\course.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
if ((fp3 = fopen("d:\\result.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
for (p1 = head; p1 != NULL; p1 = p1->next)
{
fwrite(p1, COLSIZE, 1, fp1); /*
* 保存学院信息
*/
for (p2 = p1->first; p2 != NULL; p2 = p2->next)
{
fwrite(p2, COUSRSIZE, 1, fp2); /*
* 保存课程信息
*/
for (p3 = p2->first; p3 != NULL; p3 = p3->next)
{
fwrite(p3, RESSIZE, 1, fp3); /*
* 保存课程详细信息
*/
}
}
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
}

struct col *load_file()
{
struct col *head = NULL, *p, *tail1;
struct cour *q, *tail2;
struct res *r, *tail3;
unsigned i, j;

FILE *fp1, *fp2, *fp3;

if ((fp1 = fopen("d:\\college.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
if ((fp2 = fopen("d:\\course.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
if ((fp3 = fopen("d:\\result.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
while (!feof(fp1) && !ferror(fp1))
{
p = (struct col *) malloc(sizeof(struct col));

fread(p, COLSIZE, 1, fp1);
if (!feof(fp1) && !ferror(fp1))
{
if (head == NULL)
head = p;
else
tail1->next = p;
tail1 = p;
q = (struct cour *) malloc(sizeof(struct cour));

fread(q, COUSRSIZE, 1, fp2);
p->first = q;
tail2 = q;
r = (struct res *) malloc(sizeof(struct res));

fread(r, RESSIZE, 1, fp3);
q->first = r;
tail3 = r;
for (j = 1; j < q->n; j++)
{
r = (struct res *) malloc(sizeof(struct res));

fread(r, RESSIZE, 1, fp3);
tail3->next = r;
tail3 = r;
}
tail3->next = NULL;

for (i = 1; i < p->m; i++)
{
q = (struct cour *) malloc(sizeof(struct cour));

fread(q, COUSRSIZE, 1, fp2);
tail2->next = q;
tail2 = q;
r = (struct res *) malloc(sizeof(struct res));

fread(r, RESSIZE, 1, fp3);
q->first = r;
tail3 = r;
for (j = 1; j < q->n; j++)
{
r = (struct res *) malloc(sizeof(struct res));

fread(r, RESSIZE, 1, fp3);
tail3->next = r;
tail3 = r;
}
tail3->next = NULL;
}
tail2->next = NULL;
}
}
tail1->next = NULL;

fclose(fp1);
fclose(fp2);
fclose(fp3);
return (head);

}


2007-07-23 12:51
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 

没人帮忙吗?


2007-07-23 14:06
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 
没人理我

2007-07-23 17:19
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 
我还不信了
自己顶

2007-07-23 19:34
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 
我再来

2007-07-23 23:10
melodylsp
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-7-22
收藏
得分:0 

d


2007-07-24 15:00
boot2046
Rank: 2
等 级:新手上路
威 望:3
帖 子:168
专家分:0
注 册:2007-7-23
收藏
得分:0 
#define COLSIZE (sizeof(struct col)-sizeof(struct col *)-sizeof(struct cour *))
你这是什么意思?没明白

Linux是简单的,你不需要成为天才也能理解这种简单,Windows是复杂的,就算你是天才也不能理解这种复杂
2007-07-24 15:31
boot2046
Rank: 2
等 级:新手上路
威 望:3
帖 子:168
专家分:0
注 册:2007-7-23
收藏
得分:0 
你减掉8字节,所以存的时候就那个结构的最后8字节就没有存进去,巧好你的m就在这8字节当中,所以你就读不出来了

Linux是简单的,你不需要成为天才也能理解这种简单,Windows是复杂的,就算你是天才也不能理解这种复杂
2007-07-24 15:42
快速回复:[求助]关于fread读数据时的问题
数据加载中...
 
   



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

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