   why is this not working
  [C++]I am trying to write a simple string literal to an output file. The program compiles an executes successfully, however when I open up my output file I don't see anything in it.Furthermore, if I manually write some raw text into the output file, such as "One minute I'm here...", and save it, then after I run my program and open the output file back up...the raw text is gone!I think the program is opening my output file and erasing any text in it, but somehow failing to write my string literal.Code:#include <fstream>...ofstream fout;string fName = "output.file";fout.open(fName.data(), ios::out);if(!fout.is_open()) exit(1); // i've checked and this isn't a problemfout << "Why won't this print to my output file?";fout.close();
Quote:Original post by hisDudenessI think the program is opening my output file and erasing any text in itThat is standard behaviour when opening a file in output mode. If you want to preserve pre-existing contents, you need to open it in input-and-output mode.Quote:but somehow failing to write my string literal.Check the value of fout.fail()Quote:*** Source Snippet Removed ***Use fName.c_str() rather than fName.data().   ;  also, I've never done the is_open(), but if you just do a if(fout), it will return true if it has opened.   ;Quote:Original post by Crazyfoolalso, I've never done the is_open(), but if you just do a if(fout), it will return true if it has opened.Actually, if(fout) is equivalent to if(!fout.fail()), which isn't the same thing as checking is_open().