/*
*读取文件中的数据(数据以结构体存放)
*/
#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