xceeet6 发表于 2021-12-10 15:53:58

C语言读二进制文件结构体的问题

各位大佬好!一个二进制文件的头部用一个结构体输入的,现在要读出来
结构体如下:
typedef struct IQ_header_st {
        char    MeasureUnitName;/**< Name of sensor that measured the timeseries */
        doublecenterFrequency;    /**< Center frequency in Hertz.*/
        doublesampleRate;         /**< Sample rate used for the time series. */
        uint32_t numSamples;         /**< Number of samples in the timeseries */
        doublebandwidth;         /**<bandwidth of the time series. */
        doubleattenuation;               /**< The amount of attenuation the sensor used in dB */
        doublerecordLength;
        doublelatitude;   /**< In fractional degrees, southern latitudes are negative numbers */
        doublelongitude;   /**< In fractional degrees, western longitudes are negative numbers */
        doubleelevation;   /**< In meters*/
        doublex;
        doubley;
        uint32_t timestampSeconds;   /**< Whole seconds part of the time timeseries start time */
        uint64_t timestampNSeconds;/**< Fractional seconds part of the time timeseries start time, in nano-seconds */
        int32_tdataType;         /**< The data type of the returned timeseries data, default 32 bit float real data */
}*IQ_header_t;
我用的方法读不出MeasureUnitName以后的数据,因为MeasureUnitName没有填满,centerFrequency输出的则是0,求助怎么样读出这个结构体的内容!感谢!
我的代码#include<stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


#define OPEN_ERR(fp) \
if(fp == NULL)\
{\
printf("open error\n");\
return -1;\
}\



typedef struct IQ_header_st {
        char    MeasureUnitName;/**< Name of sensor that measured the timeseries */
        doublecenterFrequency;    /**< Center frequency in Hertz.*/
        doublesampleRate;         /**< Sample rate used for the time series. */
        uint32_t numSamples;         /**< Number of samples in the timeseries */
        doublebandwidth;         /**<bandwidth of the time series. */
        doubleattenuation;               /**< The amount of attenuation the sensor used in dB */
        doublerecordLength;
        doublelatitude;   /**< In fractional degrees, southern latitudes are negative numbers */
        doublelongitude;   /**< In fractional degrees, western longitudes are negative numbers */
        doubleelevation;   /**< In meters*/
        doublex;
        doubley;
        uint32_t timestampSeconds;   /**< Whole seconds part of the time timeseries start time */
        uint64_t timestampNSeconds;/**< Fractional seconds part of the time timeseries start time, in nano-seconds */
        int32_tdataType;         /**< The data type of the returned timeseries data, default 32 bit float real data */
}*IQ_header_t;



int main()
{
        #if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
                // change code page,use utf-8
                system("chcp 65001");
        #endif
        struct IQ_header_st * IQ_header_t = (struct IQ_header_st*)malloc(sizeof(struct IQ_header_st));
        FILE* fp = fopen("C://Users//xceeet6//Desktop//电离层//newdata//20210908给蔡老师筛选频点//筛选频点//7.23//好的//35875//004.tiq", "rb");
        if (fp == NULL)
        {
                printf("File opening failure!\n");
                exit(0);
        }

//        IQ_header_ttmp;
//        fscanf(fp, "%s%d", &tmp.MeasureUnitName,&tmp.centerFrequency);
//        printf("MeasureUnitName:%s\n centerFrequency:%d\n", tmp.MeasureUnitName,tmp.centerFrequency);


        fread(IQ_header_t, sizeof(struct IQ_header_st), 1, fp);
        printf("MeasureUnitName:%s \n centerFrequency:%d \n sampleRate:%d\n numSamples:%d\n", IQ_header_t->MeasureUnitName, IQ_header_t->centerFrequency, IQ_header_t->sampleRate, IQ_header_t->numSamples);
       
//        fread(&tmp, sizeof(tmp.centerFrequency), 1, fp);
//        printf("centerFrequency:%s \n ", tmp.centerFrequency);

        printf("xc \n");
        fclose(fp);
        system("pause");
        return 0;
}

人造人 发表于 2021-12-10 16:05:27

发一下 004.tiq 这个文件

xceeet6 发表于 2021-12-10 20:11:05

001.tiq在附件里

xceeet6 发表于 2021-12-10 20:11:51

人造人 发表于 2021-12-10 16:05
发一下 004.tiq 这个文件

你好,发了,您看下。感谢

人造人 发表于 2021-12-10 20:50:02

Breakpoint 1, main () at main.c:28
28          fread(&header, 1, sizeof(header), fp);
(gdb) print header
$1 = {
MeasureUnitName = "\377\377\377\377\377\177\000\000\334\304\301\366\377\177\000\000\240\347\377\377\377\177\000\000\300\023\305\366\377\177\000\000`Ah\367\377\177\000\000\060>h\367\377\177\000\000LC_MESSA\000<\025\323)\025?\235",
centerFrequency = 6.9531439690446777e-310, sampleRate = 6.9533487149036928e-310, numSamples = 32,
bandwidth = 6.9533487449397221e-310, attenuation = 6.9533558075298694e-310, recordLength = 6.9533491762171341e-310,
latitude = 4.6355705390658865e-310, longitude = 6.9533480836669456e-310, elevation = 1.018557979860957e-312,
x = 6.9533558075274979e-310, y = 6.9533558075180118e-310, timestampSeconds = 3541384192,
timestampNSeconds = 140737354129888, dataType = -753583104}
(gdb) n
29          fclose(fp);
(gdb) print header
$2 = {MeasureUnitName = "北京站", '\000' <repeats 54 times>, centerFrequency = 7230000, sampleRate = 16384,
numSamples = 130972, bandwidth = 5.3683007093318481e-315, attenuation = 0, recordLength = -3.2927640362278245e+35,
latitude = 1.0279480544167161e+135, longitude = 5.3351225559750078e-315, elevation = -6.2088212157720166e+17,
x = -3.4010646841494705e-69, y = 2.1651320492880085e+160, timestampSeconds = 62665,
timestampNSeconds = 4859383997932765184, dataType = 1136951296}
(gdb)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct IQ_header_st {
    char MeasureUnitName; /**< Name of sensor that measured the timeseries */
    double centerFrequency;/**< Center frequency in Hertz.*/
    double sampleRate;       /**< Sample rate used for the time series. */
    uint32_t numSamples;   /**< Number of samples in the timeseries */
    double bandwidth;      /**<bandwidth of the time series. */
    double attenuation; /**< The amount of attenuation the sensor used in dB */
    double recordLength;
    double latitude; /**< In fractional degrees, southern latitudes are negative numbers */
    double longitude; /**< In fractional degrees, western longitudes are negative numbers */
    double elevation; /**< In meters*/
    double x;
    double y;
    uint32_t timestampSeconds;/**< Whole seconds part of the time timeseries start time */
    uint64_t timestampNSeconds; /**< Fractional seconds part of the time
                                 timeseries start time, in nano-seconds */
    int32_t dataType; /**< The data type of the returned timeseries data,
                         default 32 bit float real data */
} *IQ_header_t;

int main() {
    struct IQ_header_st header;
    FILE *fp = fopen("001.tiq", "rb");
    fread(&header, 1, sizeof(header), fp);
    fclose(fp);
    return 0;
}

xceeet6 发表于 2021-12-10 20:53:02

人造人 发表于 2021-12-10 20:50


老哥 你用的什么代码软件,强啊,这怎么输出的这么详细

人造人 发表于 2021-12-10 21:23:10

xceeet6 发表于 2021-12-10 20:53
老哥 你用的什么代码软件,强啊,这怎么输出的这么详细

gdb

hornwong 发表于 2021-12-11 19:22:40

{:5_95:}
页: [1]
查看完整版本: C语言读二进制文件结构体的问题