读取文件中的数据(以结构体存放)

本文介绍了一种使用C++读取特定格式二进制文件的方法,包括雷达和Vdras数据文件。通过定义结构体来存储文件中的数据,并利用iostream和fstream库进行读取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体的数据存储格式见 Blog 数据存储格式

/*
*读取文件中的数据(数据以结构体存放)
*/

#include<iostream>
#include <fstream>

//#define Field 31  //field_anal number
#define Field 15    //field_post number

using namespace std;


//the level restore certain level data
//level_z show the level 
struct Level
{
    int level_z;
    float vdras_data[64400];
};

//readSourceVdras restore the Vdras data information
struct readSourceVdras
{
    int nx;
    int ny;
    int nz;
    int field;
    char time[100];
};

//readSourceVdras restore the Vdras data information
struct readSourceRadar
{
    int nx;
    int ny;
    float radar_data[64400];
    char time[100];
};

///////////////////////////////////////////////////////////////////
//read the binary data file
void readVdras()
{
    ifstream is("001000_Vdras_bin_xiu", ios_base::in |ios_base::binary );       

    if (is)
    {
        readSourceVdras e;

        for(int j=0;j<Field;j++)         
        {
            is.read(reinterpret_cast<char *>(&e), sizeof(e));       

            printf("%s\t",e.time);
            cout << e.nx << " "<<e.ny<<" " <<e.nz<<"  field="<<e.field<<"\n";

            struct Level *v_l =(Level*) malloc(sizeof(Level)*e.nz);

            for(int i=0;i<e.nz;i++)
            {
                is.read(reinterpret_cast<char *>(&v_l[i]), sizeof(Level));

                //cout<<"\nlevel="<<v_l[i].level_z<<endl;
                //  cout<<v_l[i].vdras_data[0]<<" "<<v_l[i].vdras_data[1]<<" "<<v_l[i].vdras_data[2]<<" "<<endl;
            }


        }

        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;

    }
    else
    {
        cout << "ERROR: Cannot open file 'vdras'." << endl;
    }

    is.close();
}


//////////////////////////////////////////////////////////////////
//read the binary data file
void readRadar()
{
    ifstream is("001000_Radar_bin_xiu", ios_base::in |ios_base::binary );

    if (is)
    {
        readSourceRadar e;

        is.read(reinterpret_cast<char *>(&e), sizeof(e));

        cout << e.nx << " "<<e.ny<<endl;
        printf("%s\n",e.time);

        for(int i=0;i<5000;i++)
        {
            if(e.radar_data[i]>0)
                cout<<e.radar_data[i]<<" ";
        }
        cout<<endl;


    }
    else
    {
        cout << "ERROR: Cannot open file radar." << endl;
    }
    is.close(); 
}



int main(){

    //readVdras();
    readRadar();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值