请高手帮我看看文件读写的错误!
我写了下面一个函数:
FILE * SaveResult(struct Result result)
{
FILE *fp;
int i;
/*-------create file to save result-------------*/
if((fp = fopen("/tffs0/test_result","a+"))==NULL)
{
printf("cannot open file test_result.\n");
return NULL;
}
printf("fp = %x\n",fp);
if(fwrite(&Result,sizeof(Result),1,fp) != 1)
printf("file write error!\n");
fclose(fp);
return fp;
}
shell输出:
-> SaveResult
fp = 3fffffa0
0x3bec7260 (tShell): disk cache error: device 3bd7c850 block 243 errno c0006, disk removed while writing data, possible data loss
0x3bec7260 (tShell): disk cache error: device 3bd7c850 block 258 errno c0006, disk removed while writing data, possible data loss
value = 1073741728 = 0x3fffffa0
上面printf("fp = %x\n",fp)是我测试文件指针是否有效,按输出看fopen应该是对的阿,那么问题出在下面的fwrite吗?
[解决办法]
你看看是不是这错了:if(fwrite(&Result,sizeof(Result),1,fp) != 1),fwrite函数的第一个参数应该是result,开头字母是小写的,而你的确是大写的,小写的是结构体变量,而大写的却是结构体类型啊
[解决办法]
if(fwrite(&Result,sizeof(Result),1,fp) != 1)
&Result应该是&result吧