当前位置: 代码迷 >> C语言 >> [求助]关于fread读数据时的问题
  详细解决方案

[求助]关于fread读数据时的问题

热度:421   发布时间:2007-07-23 11:46:09.0
[求助]关于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  

----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

#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);

}


----------------解决方案--------------------------------------------------------

没人帮忙吗?


----------------解决方案--------------------------------------------------------
没人理我
----------------解决方案--------------------------------------------------------
我还不信了
自己顶
----------------解决方案--------------------------------------------------------
我再来
----------------解决方案--------------------------------------------------------

d


----------------解决方案--------------------------------------------------------
#define COLSIZE (sizeof(struct col)-sizeof(struct col *)-sizeof(struct cour *))
你这是什么意思?没明白

----------------解决方案--------------------------------------------------------
你减掉8字节,所以存的时候就那个结构的最后8字节就没有存进去,巧好你的m就在这8字节当中,所以你就读不出来了
----------------解决方案--------------------------------------------------------