1. 程式人生 > >C語言讀取二進位制檔案,讀取結果全部為零,編譯執行都沒有報錯

C語言讀取二進位制檔案,讀取結果全部為零,編譯執行都沒有報錯

利用fread讀取二進位制檔案,讀出來的結果全部為零,編譯執行都沒有報錯,程式碼如下,

有人說是大小端的問題,怎麼理解啊?判斷出來的本機器的為little endian,怎麼判斷需不需要轉換啊?要是需要轉換,怎麼轉換啊?在網上找了利用巨集處理進行大小端轉換的程式碼,但是在我這種情況下,怎麼用呢?是在fread檔案之前進行轉換麼?也不知道我程式碼裡面用的對不對···

有木有大神幫忙看一眼,問題到底出在哪?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/*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<N_ROW; 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<N_COL; 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);
}

執行結果如下:
在這裡插入圖片描述