【发布时间】:2011-09-14 13:26:26
【问题描述】:
您好,我必须将结构变量的内容写入文件。我有一个工作程序,但输出看起来失真,您可以在查看输出时理解。代码的简单版本如下,输出如下。
代码:
#include<stdio.h>
#include <stdlib.h>
struct mystruct
{
char xx[20];
char yy[20];
int zz;
};
void filewrite(struct mystruct *myvar)
{
FILE *f;
f = fopen("trace.bin","wb");
if (f == NULL)
{
printf("\nUnable to create the file");
exit(0);
}
fwrite(myvar, sizeof(struct mystruct), 1, f);
fclose(f);
}
void main()
{
struct mystruct myvar = {"Rambo 1", "Rambo 2", 1234};
filewrite(&myvar);
}
输出:
(1. 整数 '1234' 在哪里?我需要它。)
(2.这里为什么会出现一些随机字符?)
trace.bin
Rambo 1Rambo 2Ò
【问题讨论】: