把结构体数组保存到了文件中,再读取的时候总与保存的不一样
我用vc和tc分别运行了下
结果保存的文件打开里面的内容还都不一样。
代码:
#include <stdio.h>
typedef struct
{
char letter;
int weight;
int lchild;
int rchild;
int parent;
}HUFMTREE;
void main()
{
void init(HUFMTREE tree[]);
void save(HUFMTREE tree[]);
void opentree(HUFMTREE tree[]);
HUFMTREE tree[100];
int i;
scanf("%d",&i);
if(i==1)
{
init(tree);
save(tree);
}
else
{
opentree(tree);
}
}
void init(HUFMTREE tree[])
{
int i;
for(i=0;i<100;i++)
{
tree[i].lchild=0;
tree[i].rchild=0;
tree[i].parent=0;
tree[i].letter=0;
tree[i].weight=i;
}
}
void save(HUFMTREE tree[])
{
FILE *p;
p=fopen("code.txt","w");
fwrite(tree,sizeof(HUFMTREE),100,p);
fclose(p);
}
void opentree(HUFMTREE tree[])
{
FILE *p;
int i;
p=fopen("code.txt","r");
fread(tree,sizeof(HUFMTREE),100,p);
fclose(p);
for(i=0;i<100;i++)
{
printf("%d ",tree[i].weight);
}
getch();
}