#include "stdafx.h"
#include <stdio.h>
#include "stdlib.h"int main(int argc, char* argv[])
{ struct tagsps
{
int i;
char a;
};
FILE *stream; tagsps aa; aa.i = 1; aa.a = 'a';//将aa中填入数据
tagsps bb;//为了显示时读出信息
if ((stream = fopen("test.9527", "w+"))== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
fwrite(&aa, sizeof(tagsps), 1, stream);//将结构写入文件
fclose(stream);
//以下示例读出数据
if ((stream = fopen("test.9527", "r+"))== NULL)
for(int i=1;i<=1;++i)
fread(&bb, sizeof(tagsps), 1, stream);
//现在可以将bb中的数据显示出来
printf("%c\n", bb);
fclose(stream);
return 0;}

解决方案 »

  1.   

    问题中printf("%c\n", bb);应该为printf("%c\n", bb.a);
    但还是得不到字符'a'啊??用"br+" 运行出错·
    用"wr+运行不出错但是仍然得到“?”
      

  2.   

    // if ((stream = fopen("test.9527", "r+"))== NULL)
    if ((stream = fopen("test.9527", "r+"))!= NULL)
      

  3.   

    晕!呵呵谢谢你,
    另外为什么我的结构体中得 比如有成员char a[20];的话,也可以显示数据,为什么我把这个结构体中的数组开的很大(一般到500)就出错呢??
      

  4.   

    如果数组很大,建议使用 malloc或new 动态的分配空间,或者在数组定义前面加上 static 修饰