大家好,在我的项目中,我需要读取一个以short(16位值)
格式存储传感器数据的.bin文件。我使用fread
函数将数据读入缓冲区,但我感觉读入不正确。我的意思是写入和读取之间没有一致性。你们能否提供一些帮助,告诉我哪里出了问题?这不是来自我的项目代码......我只是想验证这里的fread
和fwrite
函数。
#include<stdio.h>
void main()
{
FILE *fp = NULL;
short x[10] = {1,2,3,4,5,6,5000,6,-10,11};
short result[10];
fp=fopen("c:\\temp.bin", "wb");
if(fp != NULL)
{
fwrite(x, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
rewind(fp);
fread(result, 2 /*sizeof(short)*/, 10 /*20/2*/, fp);
}
else
exit(0);
printf("\nResult");
printf("\n%d",result[0]);
printf("\n%d",result[1]);
printf("\n%d",result[2]);
printf("\n%d",result[3]);
printf("\n%d",result[4]);
printf("\n%d",result[5]);
printf("\n%d",result[6]);
printf("\n%d",result[7]);
printf("\n%d",result[8]);
printf("\n%d",result[9]);
fclose(fp)
}
我使用fread()函数后(16进制值):
temp.bin:
01 02 03 04 05 06 e1 8e 88 06 ef bf b6 0b...
执行fwrite()之后
stdout:
Result
0
914
-28
-28714
-32557
1
512
-32557
908
914