User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,612 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1760 | Replies: 1
Reply
Join Date: Oct 2007
Posts: 23
Reputation: fmufti is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
fmufti fmufti is offline Offline
Newbie Poster

Question Move file pointer to read next file value

  #1  
Nov 2nd, 2007
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();          
 }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Move file pointer to read next file value

  #2  
Nov 2nd, 2007
> 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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC