This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

fread函数不能读取.bin文件



代码如下

FILE *fid;
unsigned int bin_data[DATA_SIZE];
unsigned int bytes_read, i;

unsigned long int offset=0; 
unsigned long int temp;
unsigned int spi_val;

fid = fopen("../gpio.bin", "rb");

if (fid == NULL)
{
exit(0);
}

do

{
bytes_read = fread(bin_data, 1, DATA_SIZE, fid);
for (i=0; i<bytes_read; i++)
{
spiflash_write_byte(offset, bin_data[i]);
spi_val = spiflash_read_byte(offset);
if (bin_data[i] != spi_val)
{
printf("Error: Offset %ld reads as %2x instead of %2x\n", offset, spi_val, bin_data[i]);
}
offset++;
}
printf("Total of %ld bytes written.\n", offset);
} while (bytes_read > 0);

fclose(fid);

以上程序运行之后bytes_read为0,所以程序会直接跳出上面的循环。我用

fseek(fid,0,SEE_END);

len=ftell(fid);

计算出gpio.bin文件大小为1070个字节,所以.bin文件应该不是空的。但是用fgetc(fid)一个字节一个字节的读文件,读出来的所有数都为0xffff。

请教一下,出现这样的情况是哪里有问题啊??万分感谢