#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;}
#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;}
但还是得不到字符'a'啊??用"br+" 运行出错·
用"wr+运行不出错但是仍然得到“?”
if ((stream = fopen("test.9527", "r+"))!= NULL)
另外为什么我的结构体中得 比如有成员char a[20];的话,也可以显示数据,为什么我把这个结构体中的数组开的很大(一般到500)就出错呢??