944,014 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5694
  • C++ RSS
Nov 2nd, 2007
0

Move file pointer to read next file value

Expand Post »
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,
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fmufti is offline Offline
23 posts
since Oct 2007
Nov 2nd, 2007
0

Re: Move file pointer to read next file value

> 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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Hashing functions?
Next Thread in C++ Forum Timeline: output of graphics image in turbo c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC