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

Hi, I have implemented it as follows

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];
      Frame frame[3072];
      int j;
      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++)
                                    {
                                          for (j=0;j<10;j++)   //Limit of j needs to  be equal to no of frames
                                                {
                                                frame[i].distance[j]=(double)read_dist[j];
                                                frame[i].amplitude[j]=(double)read_ampl[j];
                                                frame[i].intensity[j]=(double)read_inten[j];
                                                frame[i].w_amplitude[j]=(double)read_w_ampl[j];
                                                }
                                     }
            if (filein.eof())break;
            }
            filein.close();
            cout<<frame[0].distance[0]<<endl;
            cout<<frame[0].distance[1]<<endl;
            delete [] read_dist;
            delete [] read_inten;
            delete [] read_ampl;
            delete [] read_w_ampl;
            
 }

But I have four questions.
1. If I have defined Frame frame[3072] but it does not work as Frame frame[ArraySize]; ?
2. How to specify the limit of 'j' as this needs to be equal to the the number of frames in the while loop.
3. The cout statement in the end crashed the program, so how to read the values ?
4. How to allocate memory frame.distance and so on ?

I have modified the code as

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 );
                  cout<<read_dist[0]<<endl; //HERE THE VALUE IS RIGHT
                   for (i=0;i<No_of_frames;i++)
                                    {
                                          frame[i].distance = new double[ArraySize];
                                          frame[i].amplitude = new double[ArraySize];
                                          frame[i].intensity = new double[ArraySize];
                                          frame[i].w_amplitude = new double[ArraySize];
                                          for (j=0;j<ArraySize;j++)
                                                {
                                                frame[i].distance[j]=(double)read_dist[j];
                                                frame[i].amplitude[j]=(double)read_ampl[j];
                                                frame[i].intensity[j]=(double)read_inten[j];
                                                frame[i].w_amplitude[j]=(double)read_w_ampl[j];
                                                }
                                     }
            if (filein.eof())break;
            }
            filein.close();
            cout<<frame[0].distance[0]<<endl; //HERE THE VALUE IS NOT RIGHT
            cout<<read_dist[0]<<endl;              //HERE THE VALUE IS NOT RIGHT

1. But the cout at the end shows the values of frame[0].distance[3072], in other words 'HERE..' I have put the comments in the code where the value is right just before the loop but not right at the end. ??

Being able to indent code would be a start. Does what you posted look like what you see in your IDE? If not, then think about it and don't just copy/paste and press submit.

FYI, never mix spaces and tabs for indenting. Everything interprets tabs differently, so the result is invariably a mess.

> cout<<frame[0].distance[0]<<endl; //HERE THE VALUE IS NOT RIGHT I'm guessing this is OUTSIDE your while loop, so it reflects the state of the last frame read, not the first frame read.
Also, there is a MASSIVE memory leak in that you don't free all the frame[i].distance = new... allocations.

Hi
I did have indenting, but while pasting code over here the indenting gets destroyed. Anyway, I got mistake of putting it outside the loop. Can you suggest how to avoid loop for each new memory creation for frame. Can't it be done as once?
Thanks,

> but while pasting code over here the indenting gets destroyed
Try harder, a lot of other people seem to manage it OK. FYI, enable the "use spaces for tabs" in your IDE and make sure you've replaced all the hard tabs with spaces.

Just move the for (i=0;i<No_of_frames;i++) and the allocations outside the while loop.

for loop to allocate all the space for frames
while loop to read the file
  for loop to copy the frames

I have modified my code as

struct Frame
{
     double frame_no;
       double dist[3072];
     double ampl[3072];
     double inten[3072];
     double w_ampl[3072];
};

struct UShortFrame
{
     unsigned frame_no;
     unsigned short dist[3072];
     unsigned short ampl[3072];
     unsigned short inten[3072];
     unsigned short w_ampl[3072];
       
};

void ReadFromFile(const char *filename, int ArraySize)
 {
     
      int i;unsigned  read_frame_no, j;
      UShortFrame* usframe = new UShortFrame;
      ifstream filein(filename, ios::in | ios::binary);
      if(!filein) {  
            std::cout << "Cannot open file to read.\n"; }
                  while (filein.read((char*)usframe, sizeof(UShortFrame)))
             {
                Frame* frame = new Frame;
                frame->frame_no = usframe->frame_no;
                for (int i = 0; i < 3072; ++i)
               {
                   frame->dist[i] = usframe->dist[i];
                   frame->ampl[i] = usframe->ampl[i];
                   frame->inten[i] = usframe->inten[i];
                   frame->w_ampl[i] = usframe->w_ampl[i];
               }    
                  if (filein.eof())break;
             }
            filein.close();
            //cout<<frame[2].dist[0]<<endl;
            delete []usframe;
           
 }

The problem is that if I want to display the value of frame[1].dist[0] how to do that at the end of all the loops. Because this is what I want to pass on to matlab later. The way its been done, it seems that every time the loop runs a frame of (frame_no, dist, ampl, inten, w_ampl) overwrites the previous frame. If not how to access any of the frame data like frame[0], frame[1] etc?

Ok, I am able to access the dist[0] and so on, but as I asked earlier how to access frame[1].dist[0] at the end of all the loops.

void ReadFromFile(const char *filename, int ArraySize)
 {
      
      int i;unsigned  read_frame_no, j;
      UShortFrame* usframe = new UShortFrame;
      ifstream filein(filename, ios::in | ios::binary);
      if(!filein) {  
            std::cout << "Cannot open file to read.\n"; }
                  while (filein.read((char*)usframe, sizeof(UShortFrame)))
             {
                Frame* frame = new Frame;
                        frame->frame_no = usframe->frame_no;
                for (int i = 0; i < 3072; ++i)
               {
                   frame->dist[i] = usframe->dist[i];
                   frame->ampl[i] = usframe->ampl[i];
                   frame->inten[i] = usframe->inten[i];
                   frame->w_ampl[i] = usframe->w_ampl[i];
                           
               }
                  cout<<(*frame).dist[0]<<endl;    
                  if (filein.eof())break;
                  }
            filein.close();
            cout<<(*frame).dist[0]<<endl;  //WANT TO ACCESS ANY FRAME
            delete []usframe;
            
 }

frame[1].dist[0]

This syntax implies that frame is a object for which the [] operator is overloaded-----for example and array, a vector or maybe a map. Assuming it's an array based on your post then frame could be declared one of two ways:

Frame frame[MAXSIZE];
Frame * frame = new Frame[MAXSIZE];

where MAXSIZE is a const int. The first version is sometimes called a static array because it's declared using static memory and the second version is sometimes called a dynamic array because it's declared with dynamic memory.

On the other hand, this:

Frame* frame = new Frame;

declares frame as a single object of type Frame. To access a given element of the dist array within frame using this syntax you would do something like this:

frame->dist[x];

And if frame were declared as a single Frame object using static memory like this:

Frame frame;

Then you would access a given dist element like this:

frame.dist[x];

This article has been dead for over six months. Start a new discussion instead.