Reassigning existing object with ios::app ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Reassigning existing object with ios::app ?

 
0
  #1
Jan 26th, 2008
Good evening ladies and gents,

Had a question concerning this piece of code that I'm trying out from a book, it supposed to open a file, write text to it, close it, reopen it, append text to it and then write the text from the file to the console screen, it works fine up untill the text has to be written to the console screen, then I get the message that the file can't be opened for reading though the text is added to the file?

What am I doing wrong?

  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main() // returns 1 on error
  7. {
  8. char fileName[80];
  9. char buffer[255]; // for user input.
  10. cout << "Please reenter the file name: ";
  11. cin >> fileName;
  12.  
  13. ifstream fin(fileName);
  14.  
  15. if (fin) // allready exists ?
  16. {
  17. cout << "Current file contents:\n";
  18. char ch;
  19.  
  20. while (fin.get(ch))
  21. cout << ch;
  22. cout << "\n***End of file contents.***\n";
  23. }
  24. fin.close();
  25.  
  26. cout << "\nOpening " << fileName << " in append mode...\n";
  27.  
  28. ofstream fout(fileName, ios::app);
  29. if (!fout)
  30. {
  31. cout << "Unable to open " << fileName << " for appending !\n";
  32. return 1;
  33. }
  34.  
  35. cout << "\nEnter text for this file: ";
  36. cin.ignore(1, '\n');
  37. cin.getline(buffer, 255);
  38. fout << buffer << "\n";
  39. fout.close();
  40.  
  41. fin.open(fileName); // reassign existing fin object.
  42. if (!fin) // <-- problem is here.
  43. {
  44. cout << "Unable to open " << fileName << " for reading !\n";
  45. return 1;
  46. }
  47.  
  48. cout << "\nHere's the contents of the file: \n";
  49. char ch;
  50. while (fin.get(ch))
  51. cout << ch;
  52. cout << "\n***End of file contents.***\n";
  53. fin.close();
  54.  
  55. return 0;
  56. }
Last edited by JoBe; Jan 26th, 2008 at 4:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Reassigning existing object with ios::app ?

 
0
  #2
Jan 26th, 2008
Well, found a solution to solve the problem:

  1. if (fin) // allready exists ?
  2. {
  3. cout << "Current file contents:\n";
  4. char ch;
  5.  
  6. while (fin.get(ch))
  7. cout << ch;
  8. cout << "\n***End of file contents.***\n";
  9. }
  10. fin.close();
  11. fin.clear();
  12.  
  13. .......

The problem seems to be this:
fin returns 0 because "the base class iso (from which istream is inherited) provides an overloaded cast operator that converts a stream into a pointer of type void*. The value of the pointer is 0 if an error occurred while attempting to read a value or the end-of-file indicator was encoutered." (1)
So, If I understand this correctly, your input because of being casted to a type void pointer get's another address and therefore isn't recognised?

If so, why does the function clear() solve this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Reassigning existing object with ios::app ?

 
1
  #3
Jan 27th, 2008
When you read the file completely through the first time, the EOF flag was set in the object fin. That flag stays set until you use the fin.clear( ) statement. The act of closing, or reopening, the file does not clear the flag.

This does not really have anything to do with casting of the stream operator. However, that casting to a 0 upon error or end of file, non-zero otherwise, is handy for loop control when reading or for testing successful opening of files, as your code uses.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Reassigning existing object with ios::app ?

 
0
  #4
Jan 27th, 2008
Ok, thanks for the explanation vmanes.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC