c语言fread读取错误,【求助】C语言fread读取二进制文件时,读取结果全都是零
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("創(chuàng)建輸出文件失敗!\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);
}
運行結果:
《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的c语言fread读取错误,【求助】C语言fread读取二进制文件时,读取结果全都是零的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言循环程序设计教案,10 《C语言程
- 下一篇: 用c语言编程实现strcpy,用c语言.