C++: ifstream file pointer not getting reset

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

Join Date: Feb 2005
Posts: 2
Reputation: samarth is an unknown quantity at this point 
Solved Threads: 0
samarth samarth is offline Offline
Newbie Poster

C++: ifstream file pointer not getting reset

 
0
  #1
Feb 23rd, 2005
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

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

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

 
0
  #2
Feb 23rd, 2005
Use code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 26
Reputation: gastonci is an unknown quantity at this point 
Solved Threads: 4
gastonci gastonci is offline Offline
Light Poster
 
0
  #3
3 Days Ago
The problem may be in the bits of the stream. Have you try file.clear()?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC