Why does instream sometimes create files but can't write to them?

I am trying to use the outstream class to write some stuff to a file, but all that happens is that the file is created and then nothing. I have just a code:

#include <iostream>
#include <fstream>

#include <cstring>
#include <cerrno>

#include <time.h>

using namespace std;

int main(int argc, char* argv[])
{
    ofstream file;
    file.open("test.txt");
    if (!file) {
        cout << strerror(errno) << endl;
    } else {
        cout << "All is well!" << endl;
    }

    for (int i = 0; i < 10; i++) {
        file << i << "\t" << time(NULL) << endl;
    }

    file.flush();
    file.close();
    return 0;
}

      

When I create a console application everything works fine, so I am afraid this code is not completely representative. However, I am using code like this in a much larger project which, to be honest, I do not quite understand (Neurostim). I have to write some class which is compiled into dll that Neurostim can load.

When the code runs, "test.txt" is generated and then "No error!" printed, as it appears to be the result of fear. This is obviously wrong. The application works in a completely different way and is not step by step due to the fact that I am trying to write a corrupted stream. He just doesn't do it. It seems to me that there is no problem with the permissions because the file is actually created.

Does anyone have any idea what things might be causing this strange behavior? (I'm on WinXP Pro SP3 and using Visual C ++ 2008 Express Edition)

Thank!

+2


source to share


3 answers


Just a thought: - in your real code, are you reusing your stream object?



If so, you need to make sure that you are calling clear()

into the thread before reusing the object otherwise, if the previous error state, it won't work. As I recall, when called clear()

in such a stream, there would be no empty file that could not be written to, as you described in your question.

+4


source


    ofstream file;
    file.open("test.txt");

      

Just nit: you can combine this into one line. ofstream file("test.txt");

    if (file) {
            cout << strerror(errno) << endl;
    } else {
            cout << "All is well!" << endl;
    }

      



Your test is back. If file

true, it is open and ready for writing.

Also, I will not assume that strerror () works correctly on Windows. Most Windows APIs do not use errno to signal errors. If your failure occurs outside of the C / C ++ runtime library, this may not tell you anything interesting.

+2


source


UPDATE . After thinking about it, the file cannot be opened with fstreams

errno

. It is possible that this errno

ends up on some platforms (especially if those platforms implement operations fstream

on FILE*

or on file descriptors or another library that installs errno

), but this is not guaranteed. The official way to check for failure is with exceptions , std::io_state

or helper methods on std::fstream

(for example fail

or bad

). Unfortunately, you cannot get as much information from std::streams

as you can from errno

.

You have a wrong if statement. operator void*

returns NULL

(aka false

) if the file is not writable. It returns nonzero (aka true

) if the file can be written. So you want:

if (!file) {
        cout << strerror(errno) << endl;
} else {
        cout << "All is well!" << endl;
}

      

Or:

if (!file.good()) {
        cout << strerror(errno) << endl;
} else {
        cout << "All is well!" << endl;
}

      

0


source







All Articles