| | |
Reassigning existing object with ios::app ?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?
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?
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; int main() // returns 1 on error { char fileName[80]; char buffer[255]; // for user input. cout << "Please reenter the file name: "; cin >> fileName; ifstream fin(fileName); if (fin) // allready exists ? { cout << "Current file contents:\n"; char ch; while (fin.get(ch)) cout << ch; cout << "\n***End of file contents.***\n"; } fin.close(); cout << "\nOpening " << fileName << " in append mode...\n"; ofstream fout(fileName, ios::app); if (!fout) { cout << "Unable to open " << fileName << " for appending !\n"; return 1; } cout << "\nEnter text for this file: "; cin.ignore(1, '\n'); cin.getline(buffer, 255); fout << buffer << "\n"; fout.close(); fin.open(fileName); // reassign existing fin object. if (!fin) // <-- problem is here. { cout << "Unable to open " << fileName << " for reading !\n"; return 1; } cout << "\nHere's the contents of the file: \n"; char ch; while (fin.get(ch)) cout << ch; cout << "\n***End of file contents.***\n"; fin.close(); return 0; }
Last edited by JoBe; Jan 26th, 2008 at 4:46 pm.
Well, found a solution to solve the problem:
The problem seems to be this:
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?
C++ Syntax (Toggle Plain Text)
if (fin) // allready exists ? { cout << "Current file contents:\n"; char ch; while (fin.get(ch)) cout << ch; cout << "\n***End of file contents.***\n"; } fin.close(); fin.clear(); .......
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)
If so, why does the function clear() solve this?
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: realloc malloc and Heap problem.
- Next Thread: exception throwing on file accesing error
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






