Move file pointer to read next file value

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 23
Reputation: fmufti is an unknown quantity at this point 
Solved Threads: 0
fmufti fmufti is offline Offline
Newbie Poster

Move file pointer to read next file value

 
0
  #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,
  1. struct Frame
  2. {
  3. double frame_no;
  4. double dist[3072];
  5. double ampl[3072];
  6. };
  7.  
  8. struct UShortFrame
  9. {
  10. unsigned frame_no;
  11. unsigned short dist[3072];
  12. unsigned short ampl[3072];
  13.  
  14. };
  15.  
  16. void ReadFromFile(int counter, char* stop)
  17. {
  18.  
  19. int i; static const char filename[] = "data.dat";
  20.  
  21. UShortFrame* usframe = new UShortFrame;
  22. ifstream filein(filename, ios::in | ios::binary);
  23. if(!filein) {
  24. std::cout << "Cannot open file to read.\n"; }
  25. if(filein.read((char*)usframe, sizeof(UShortFrame)))
  26. {
  27. Frame* frame = new Frame;
  28. frame->frame_no = usframe->frame_no;
  29. for (int i = 0; i < 3072; ++i)
  30. {
  31. frame->dist[i] = usframe->dist[i];
  32. frame->ampl[i] = usframe->ampl[i];
  33. }
  34.  
  35. delete []usframe;
  36. }
  37.  
  38. filein.close();
  39. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Move file pointer to read next file value

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC