码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-22 22:18:47      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:结构体   数据   文件   读取   

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

#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;
}

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

标签:结构体   数据   文件   读取   

原文地址:http://blog.csdn.net/omenglishuixiang1234/article/details/45200287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!