鱼C论坛

 找回密码
 立即注册
查看: 933|回复: 7

[已解决]C语言读二进制文件结构体的问题

[复制链接]
发表于 2021-12-10 15:53:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大佬好!一个二进制文件的头部用一个结构体输入的,现在要读出来
结构体如下:
typedef struct IQ_header_st {
        char    MeasureUnitName[64];  /**< 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;
我用的方法读不出MeasureUnitName[64]以后的数据,因为MeasureUnitName[64]没有填满,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[64];  /**< 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()
{
        #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_t  tmp;
//        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 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[64]; /**< 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-10 16:05:27 | 显示全部楼层
发一下 004.tiq 这个文件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-10 20:11:05 | 显示全部楼层
001.tiq在附件里

001.zip

506.44 KB, 下载次数: 1

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-10 20:11:51 | 显示全部楼层
人造人 发表于 2021-12-10 16:05
发一下 004.tiq 这个文件

你好,发了,您看下。感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[64]; /**< 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-10 20:53:02 | 显示全部楼层

老哥 你用的什么代码软件,强啊,这怎么输出的这么详细
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-10 21:23:10 | 显示全部楼层
xceeet6 发表于 2021-12-10 20:53
老哥 你用的什么代码软件,强啊,这怎么输出的这么详细

gdb
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-11 19:22:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 07:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表