【问题标题】:Writing and reading binary files in C用 C 编写和读取二进制文件
【发布时间】:2019-03-27 13:37:18
【问题描述】:

我正在编写一个 C 代码,它写入一些信号采样信息,然后读取它们并将它们存储到二进制文件中。我在写信息时得到了正确的输出,但在尝试读取它们时出现了错误。这是我的代码:

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

typedef struct {
    unsigned int sample_offset;
    double time;
    unsigned int date;
    unsigned char receiver_name[20];
    double sample_rate;
    unsigned int channel;
    unsigned int bits;
    unsigned char file_type[11];
    unsigned int freq_band;
    double channlel_bandwidth;
    double firmwire_version;
    double header_version;
} sampleInfo;

int main() {
    // Writing file
    FILE *fw;
    sampleInfo data = { 64, 0.55, 19, "Gadfly", 51.2, 2, 4, "Spreadsheet", 1,24, 1.0, 1.0 }, read_data;

    fw = fopen("sample.bin", "wb");
    if (!fw) {
        printf("Unable to open the file\n");
        return 1;
    } else {
        printf("Sample offset: %d bytes\n", data.sample_offset);
        printf("Capture time: %.2f seconds\n", data.time);
        printf("Date: %d October,2018\n", data.date);
        printf("Receiver name: %s\n", data.receiver_name);
        printf("Sample rate: %.2f Mega-samples per second\n", data.sample_rate);
        printf("Number of channels used: %d\n", data.channel);
        printf("Number of bits per I and Q sample: %d\n", data.bits);
        printf("File type: %s\n", data.file_type);
        printf("Frequency band per channel: L%d\n", data.freq_band);
        printf("Channel Bandwidth: %.fMHz\n", data.channlel_bandwidth);
        printf("Firm-wire version: %.1f\n", data.firmwire_version);
        printf("Header version: %.1f\n\n\n\n", data.header_version);
    }

    fwrite(&data, sizeof(sampleInfo), 1, fw);
    fclose(fw);

    // Reading file
    FILE *fr;
    fr = fopen("Sample.bin", "rb");
    if (!fr) {
        printf("Unable to open the file\n");
        return 1;
    } else {
        printf("Sample offset: %d bytes\n", read_data.sample_offset);
        printf("Capture time: %.2f seconds\n", read_data.time);
        printf("Date: %d October,2018\n", read_data.date);
        printf("Receiver name: %s\n", read_data.receiver_name);
        printf("Sample rate: %.2f Mega-samples per second\n", read_data.sample_rate);
        printf("Number of channels used: %d\n", read_data.channel);
        printf("Number of bits per I and Q sample: %d\n", read_data.bits);
        printf("File type: %s\n", read_data.file_type);
        printf("Frequency band per channel: L%d\n", read_data.freq_band);
        printf("Channel Bandwidth: %.fMHz\n", read_data.channlel_bandwidth);
        printf("Firm-wire version: %.1f\n", read_data.firmwire_version);
        printf("Header version: %.1f\n\n\n\n", read_data.header_version);
    }

    fread(&read_data, sizeof(sampleInfo), 1, fw);
    fclose(fr);

    return 0;
}

我对 C 语言比较陌生,所以在这方面有点挣扎。任何形式的帮助将不胜感激。提前致谢。

【问题讨论】:

  • 感谢您的检测。我已经解决了这个问题,现在它可以工作了!

标签: c file binary


【解决方案1】:

打印前阅读!

在打印文本之前,fread(&amp;read_data,sizeof(sampleInfo),1, fr); 必须位于 else 分支中。

【讨论】:

    【解决方案2】:

    阅读语句有错别字:fread(&amp;read_data, sizeof(sampleInfo), 1, fw); 应该读自fr,而不是fw:

    fread(&read_data, sizeof(sampleInfo), 1, fr);
    

    此外,您应该在打印read_data的内容之前阅读数据。

    您还应该测试fwrite 和fread 的返回值,以验证I/O 操作是否成功。

    【讨论】:

    • 哦,确实,连那个错别字都没发现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多