日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

c语言fread无法存储,【求助】C语言fread读取二进制文件时,读取结果全都是零

發布時間:2023/11/27 生活经验 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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("創建輸出文件失敗!\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读取二进制文件时,读取结果全都是零的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。