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);
}

运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38742627/article/details/85693984