0

OK, here is a problem that I have found described many times on the net, but have not found a definitive solution. Until recently, file writing has worked just fine for me, but no longer.

The questions are:
Why does it not work now?
What might I have done somewhere else in the code to break it?
How do I fix it?

My full code is over 10,000 lines long, so I really cannot provide it all here. But here is the issue:

       string logfilename="Log/test.err";
       fstream filename;
       filename.open(logfilename.c_str(),fstream::app);
       filename << "Start Log\n";
       filename << std::flush;
       filename.close();

This code creates the file test.err in the Log folder (if it was missing, it will also create a missing Log folder). But the file remains empty. It fails to write "Start Log" into the file.

However. . .

           string logfilename="Log/test.err";
           fstream filename;
           filename.open("Log/test.err",fstream::app);
           filename << "Start Log\n";
           filename << std::flush;
           filename.close();

This works just fine. Notice that the only difference is that I replaced the variable logfilename with its contents. Since fstream.open really wants a char, I tried this. . .

   char *nn =(char*)logfilename.c_str();
   fstream filename;
   filename.open(nn, fstream::app);
   filename << "Start Log\n";
   filename << std::flush;
   filename.close();

This code works just like the first code. It creates the file if it was missing, but fails to write anything into it.

I have tried these three versions with filename.open(nn, fstream::out) with no change in the outcome (although in this application I really want a function that appends data to the file). I have also tried this without the "Log/" in the file name, so that the file would be in the active folder. Still no difference in outcome. I have also tried this with the target file already made (but empty). Still nothing is written to the file.

I have #include <fstream> at the start of each program (wouldn't really compile without that).

I am using MinGW to compile this as a C++ program. No error messages and no warnings occur during compilation.

I've read the fstream tutorials (on this site and others) and as far as I can tell I am following the directions.

So, what's going on?

2
Contributors
5
Replies
24
Views
2 Years
Discussion Span
Last Post by tinstaafl
1

The problem appears to be that you aren't telling filename whether to open the file for extraction or insertion. You need to combine fstream::out with fstream:app with the bitwise OR operator:

filename.open(logfilename.c_str(), fstream::out | fstream::app);
0

Hmm,
Well, that seems to work. Thank you.

But, onto the more philosophical questions:

Why did it work the way I had it before, but not now?

And/Or

Why did it work when I supplied the file name "Log/test.err" directly, but not when it was supplied as a string variable?

I suppose we're getting into my naive status with regard to C++ memory handling, but what could I have possibly done to the code that caused the function to break the way I supplied it above? I ask this because I suspect that there may be a fundamental flaw hidden somewhere within my code.

1

One possibily is changing compilers. For instance using VS2013 your original code works fine, but using Code::Blocks with Cygwin GCC it doesn't work until the out option is used.

0

Again Hmmm. Thanks again.

I guess I did change compilers. From the Ancient Borland to the more modern MinGW.

Is there any way to know compilers differ on these types of issues a priori? What other surprises will I find in this transition?

I am going to mark the question solved now, but I am still interested in furthering this discussion.

0

The docs for each compiler might have some info on differences. In your case though the difference is probably because you upgraded to a newer more standard compliant compiler.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.