Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言编写和读取二进制文件_C_File_Binary - Fatal编程技术网

用C语言编写和读取二进制文件

用C语言编写和读取二进制文件,c,file,binary,C,File,Binary,我正在编写一个C代码,它写入一些信号采样信息,然后读取它们并将它们存储到一个二进制文件中。我在写信息时得到了正确的输出,但在尝试读取信息时出错。这是我的密码: #include <stdio.h> #include <stdlib.h> typedef struct { unsigned int sample_offset; double time; unsigned int date; unsigned char receiver_na

我正在编写一个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;
}
#包括
#包括
类型定义结构{
无符号整数采样偏移量;
双倍时间;
无符号整数日期;
未签名字符接收器名称[20];
双采样率;
无符号整数通道;
无符号整数位;
无符号字符文件_类型[11];
无符号整数频率带;
双通道带宽;
双固网型;
双标题_版本;
}样本信息;
int main(){
//写入文件
文件*fw;
sampleInfo数据={64,0.55,19,“牛虻”,51.2,2,4,“电子表格”,1,24,1.0,1.0},读取数据;
fw=fopen(“样品箱”、“wb”);
如果(!fw){
printf(“无法打开文件\n”);
返回1;
}否则{
printf(“样本偏移量:%d字节\n”,data.Sample\u偏移量);
printf(“捕获时间:%.2f秒\n”,data.time);
printf(“日期:%d 2018年10月\n”,数据日期);
printf(“接收方名称:%s\n”,数据。接收方名称);
printf(“采样率:每秒%.2f百万个样本”,数据.Sample\u率);
printf(“使用的通道数:%d\n”,data.channel);
printf(“每个I和Q样本的位数:%d\n”,data.bits);
printf(“文件类型:%s\n”,数据文件类型);
printf(“每个通道的频带:L%d\n”,数据频率带);
printf(“信道带宽:%.fMHz\n”,数据通道带宽);
printf(“硬线版本:%.1f\n”,数据为.firmwire\u版本);
printf(“页眉版本:%.1f\n\n\n”,data.Header\u version);
}
fwrite(&data,sizeof(sampleInfo),1,fw);
fclose(fw);
//读取文件
文件*fr;
fr=fopen(“样品箱”、“rb”);
如果(!fr){
printf(“无法打开文件\n”);
返回1;
}否则{
printf(“样本偏移量:%d字节\n”,读取数据。样本偏移量);
printf(“捕获时间:%.2f秒\n”,读取数据时间);
printf(“日期:%d 2018年10月”,读取数据日期);
printf(“接收方名称:%s\n”,读取数据。接收方名称);
printf(“采样率:每秒%.2f兆采样数”,读取数据。采样率);
printf(“使用的通道数:%d\n”,读取数据.channel);
printf(“每个I和Q样本的位数:%d\n”,读取数据位);
printf(“文件类型:%s\n”,读取数据。文件类型);
printf(“每个通道的频带:L%d\n”,读取数据。频带);
printf(“信道带宽:%.fMHz\n”,读取数据。信道带宽);
printf(“固网版本:%.1f\n”,读取数据。固网版本);
printf(“标题版本:%.1f\n\n\n”,读取数据。标题版本);
}
fread(&read_数据,sizeof(sampleInfo),1,fw);
fclose(fr);
返回0;
}
我是C语言的新手,所以在这方面有点困难。我们将非常感谢您的任何帮助。提前感谢。

打印前请阅读


fread(&read_数据,sizeof(sampleInfo),1,fr)必须位于
else
分支中。

阅读语句中有一个输入错误:
fread(&read_data,sizeof(sampleInfo),1,fw)应从
fr
读取,而不是从
fw
读取:

fread(&read_data, sizeof(sampleInfo), 1, fr);
此外,在打印
read_data
的内容之前,您应该先读取数据


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

哦,事实上,您甚至没有发现该输入错误。感谢您的检测。我已经修复了这个,现在它正在工作!