944,004 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 18775
  • C++ RSS
Feb 23rd, 2005
0

C++: ifstream file pointer not getting reset

Expand Post »
Hi,

I have the following piece of code. I do the following here

1)open a file.
2) Print the filepointer positon . I get 0
3)Read the file in a while loop till i reach the end
4)close the file
5)Open the same file
6) Print the file pointer position I get -1.( I expect 0 here)

Can anyone let me know the reason for this error and how is it possible to avoid this.

Thanking you in advance
Samarth

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. int OpenFile(fstream &fileHandle, char * fileName)
  6. {
  7. cout << "Opening File" << endl;
  8. int success = true;
  9.  
  10. // Open the file and read the data
  11. fileHandle.open(fileName, ios::in|ios::binary);
  12.  
  13. // Check if the file is present
  14. if (!fileHandle.is_open())
  15. {
  16. // File is not present. Log the information
  17. cout << "OpenFile::Error opening file "<< endl;
  18. success = false;
  19. }
  20.  
  21. return success;
  22. }
  23.  
  24. int ReadFile(char *buf, fstream &file)
  25. {
  26. while(!file.eof())
  27. {
  28. file.read(buf, 1024);
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. fstream file;
  35. OpenFile(file, "1.mpg");
  36. cout << "tellg " << file.tellg()<< endl;
  37.  
  38. char buf[1024];
  39. ReadFile(buf,file);
  40. file.close();
  41.  
  42. //file = new ifstream();
  43. OpenFile(file, "1.mpg");
  44. cout << "tellg " << file.tellg()<< endl;
  45.  
  46. return 1;
  47. }
Last edited by alc6379; Feb 23rd, 2005 at 6:16 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
samarth is offline Offline
2 posts
since Feb 2005
Feb 23rd, 2005
0

Re: C++: ifstream file pointer not getting reset

Use code tags
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 2nd, 2009
0
Re: C++: ifstream file pointer not getting reset
The problem may be in the bits of the stream. Have you try file.clear()?
Reputation Points: 10
Solved Threads: 4
Light Poster
gastonci is offline Offline
34 posts
since Aug 2009

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: Have trouble with a dijkstra program
Next Thread in C++ Forum Timeline: Need help with task!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC