Hi
I am reading a file with number of frames.
frame 1
distance [3072]
intensity [3072]
amplitude [3072]
w_amplitude [3072]
frame 2...
Now I want to read the data in structure form so that I have:-
frame[0].distance[0]
frame[0].distance[1]....
...
frame[0].amplitude[0]
...
frame[1].distance[0]
and so on.
My code is below and I have placed comment where I think the structure result should come so that at the end I can pass this structure to MATLAB.
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
void ReadFromFile(const char *filename, int ArraySize);
int main()
{
static const char filename[] = "data.dat";
int ArraySize=3072;
ReadFromFile(filename, ArraySize);
return 0;
}
void ReadFromFile(const char *filename, int ArraySize)
{
int i;unsigned read_frame_no;
unsigned short *read_dist=new unsigned short[ArraySize], *read_ampl=new unsigned short[ArraySize],
*read_inten=new unsigned short[ArraySize], *read_w_ampl=new unsigned short[ArraySize];
double *distance=new double[ArraySize], *intensity=new double[ArraySize];
double *amplitude=new double[ArraySize], *w_amplitude=new double[ArraySize];
ifstream filein(filename, ios::in | ios::binary);
if(!filein) {
std::cout << "Cannot open file to read.\n"; }
while ( filein.read((char *) &read_frame_no, sizeof(unsigned))) {
filein.read((char *) read_dist, sizeof( unsigned short ) * ArraySize );
filein.read((char *) read_inten, sizeof( unsigned short ) * ArraySize );
filein.read((char *) read_ampl, sizeof( unsigned short ) * ArraySize );
filein.read((char *) read_w_ampl, sizeof( unsigned short ) * ArraySize );
for (i=0;i<ArraySize;i++)
{
distance[i]=(double)read_dist[i];
amplitude[i]=(double)read_ampl[i];
intensity[i]=(double)read_inten[i];
w_amplitude[i]=(double)read_w_ampl[i];
}
//Somewhere here I want to store it as structure format
if (filein.eof())break;
}
filein.close();
delete [] read_dist;
delete [] read_inten;
delete [] read_ampl;
delete [] read_w_ampl;
}