Hi, I have the following structure to read the file data with fixed format of my binary file. Now I want that every time my ReadFromFile function is called with 'counter' variable it should
1. return me frame specified in the 'counter'.
2. The file needs to be closed only when 'stop' has value say 'STOP'
3. The file should not be opened every time and be opened once only.

Thanks,

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

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

void ReadFromFile(int counter, char* stop)
 {
     
      int i; static const char filename[] = "data.dat";

      UShortFrame* usframe = new UShortFrame;
      ifstream filein(filename, ios::in | ios::binary);
      if(!filein) {  
         std::cout << "Cannot open file to read.\n"; }
          if(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];
                    }    
            
		delete []usframe;
		}
            
		filein.close();          
 }

> UShortFrame* usframe = new UShortFrame;
> delete []usframe;
You didn't allocate an array, so drop the [ ] from the delete.

> 1. return me frame specified in the 'counter'.
> 2. The file needs to be closed only when 'stop' has value say 'STOP'
> 3. The file should not be opened every time and be opened once only.
Sounds like you need a 'FileFrame' class of some sort.
- the constructor opens the file
- the destructor closes the file
- a member function to get the next frame

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.