C语言fread读取二进制文件时,读取结果全都是零,编译运行都没问题,但是就是结果显示不对,猜想可能有几个原因:
1. 大小端没处理好,设置了程序判断机器为little endian,但是,身为小白的我不知大小端转换怎么换,应该是在fread之前转换,还是在fread之后?我下面程序中的转换不知道对不对啊
😂而且什么时候应该做大小端转换呢?
2.用printf打印显示会不会有问题?目标文件是以short 格式存储的二进制文件,fread读取之后直接printf会不会有问题?
代码如下,希望有大神帮忙看一下有什么问题···
#include
#include
#include
#include
/*define the row*column of the image file*/
#define N_ROW 1
#define N_COL 9
/*swap the little/big endian of bytes*/
#define SWAP_2(x) ( (((x) & 0xff) << 8) | ((unsigned short)(x) >> 8) )
#define SWAP_4(x) ( ((x) << 24) | \
(((x) << 8) & 0x00ff0000) | \
(((x) >> 8) & 0x0000ff00) | \
((x) >> 24) )
#define FIX_SHORT(x) (*(unsigned short *)&(x) = SWAP_2(*(unsigned short *)&(x)))
#define FIX_INT(x) (*(unsigned int *)&(x) = SWAP_4(*(unsigned int *)&(x)))
#define FIX_FLOAT(x) FIX_INT(x)
int is_big_endian_();
void swap_slc_data(short *cdata);
int main()
{
FILE *fp_in=NULL, *fp_out=NULL;
int i, j, num_read, swap=0;
float real, imag;
double *amp=NULL;
float *phase=NULL;
long num_fseek;
short *tmp=NULL;
//create the txt outfile
if ((fp_out = fopen("IMGtest1_out.txt", "wt")) == NULL)
{
printf("创建输出文件失败!\n");
return 0;
}
printf("***outfile fopen ok! ***\n");
//open the binary SLCfile
if ((fp_in = fopen("IMGtest1.SLC", "rb")) == NULL)
{
printf("打开输入文件失败!\n");
return 0;
}
printf("*** fopen ok! ***\n");
//allocate the memory for one row
//tmp = (short *)malloc(2 * n_col * sizeof(short));
if((tmp = (short *)malloc(2*N_COL*sizeof(short))) == NULL)
{
printf("分配内存错误!\n");
free(tmp);
return 0;
}
if((amp = (double *)malloc(N_COL*N_ROW*sizeof(double))) == NULL)
{
printf("分配内存错误!\n");
free(amp);
return 0;
}
/*if((phase = (float *)malloc(N_COL*N_ROW*sizeof(float))) == NULL)
{
printf("分配内存错误!\n");
free(phase);
return 0;
}*/
printf("*** malloc ok! ***\n");
/*check the bigendian of litte endian*/
if (is_big_endian_() == -1) {swap = 1;fprintf(stderr,".... little endian,swapping bytes\n");} else {swap = 0;}
//read data
for (i=0; i
{
/*change the big/little endian*/
if (swap) swap_slc_data(tmp);
//set the starting read position, from the beginning
num_fseek = i*2*N_COL*sizeof(short);
fseek(fp_in, num_fseek, SEEK_SET);
printf("*** fseek ok! ***\n");
//readdata row by row
num_read = fread(&tmp[0], sizeof(short), 2*N_COL, fp_in);
if (num_read != 2*N_COL)
{
printf("读取文件失败!\n");
return 0;
}
printf("*** fread ok! %d data is read ***\n", num_read);
//基于读取出的一行提取实部、虚部,并计算相位和幅度
for(j=0; j
{
real = FIX_SHORT(tmp[2*j]);
imag = FIX_SHORT(tmp[2*j+1]);
printf("real[%d]: %f\timag[%d]: %f\t",j,real,j,imag);
amp[j+N_COL*i] = (int)sqrt(real*real + imag*imag);
printf("amp[%d][%d]: %f\n", i, j, amp[j+N_COL*i]);
/*phase[i][j] = (float)atan(imag/real);
printf("phase[%d][%d]: %f\t", i, j, phase[i][j]);*/
//write into .txtfile
fprintf(fp_out, "%f\t", amp[j+N_COL*i]);
}
fprintf(fp_out, "\n");
printf("\n");
}
free(tmp);
free(amp);
/*free(phase);*/
fclose(fp_out);
fclose(fp_in);
return 0;
}
/*---------------------------------------------------------------*/
/* check endian of machine */
/* 1 if big; -1 if little */
int is_big_endian_()
{
union
{
long l;
char c[sizeof (long) ];
} u;
u.l = 1;
return( u.c[sizeof(long) - 1] == 1 ? 1 : -1);
}
/*--------------------------------------------------------------*/
/* swap little/big endian */
void swap_slc_data(short *cdata)
{
FIX_SHORT(cdata);
}
运行结果:
如果觉得《c语言fread读取错误 【求助】C语言fread读取二进制文件时 读取结果全都是零》对你有帮助,请点赞、收藏,并留下你的观点哦!