4

I've been having strange problems writing to file with ofstreams and have now that

ofstream.fail()

is returning true right after my ofstream.open() call.

Are there some ways to get additional information, like more specifics on why the fail state was set?

EDIT, additional information: is_open() returns false.

user788171
  • 16,753
  • 40
  • 98
  • 125
  • what does `is_open()` say? – jrok Apr 03 '13 at 12:13
  • is_open() returns false. – user788171 Apr 03 '13 at 12:14
  • According to this.. http://www.cplusplus.com/reference/ios/ios/fail/ I guess the function will not help you to dig into the actual cause of the fail... – Hiren Pandya Apr 03 '13 at 12:15
  • 1
    Are you writing to a file that exists? Change the name of the file to a non-existing one and see if that works. If not, then you either are looking at the wrong directory or you don't have write permissions on the directory. If it does work, then you don't have write permissions on the file itself. – Joseph Mansfield Apr 03 '13 at 12:19
  • 1
    On a UNIX system try looking at errno, on Windows check GetLastError() value. These are not part of the C++ standard but may help anyway. – brian beuning Apr 03 '13 at 12:23
  • File does exist, I am opening in append mode so that should be fine, especially since I call close after each write attempt. I do have write permissions because I can write to that file from terminal. There are also sufficient number of file descriptors on the system. I guess I am hoping for something from the system kernel that will tell me more precisely why it failed as I have checked all the usual suspects. – user788171 Apr 03 '13 at 12:25

4 Answers4

1

Make sure that ofstream does not have a file associated with it already (opened a file with that stream already). According to http://www.cplusplus.com/reference/fstream/ofstream/open/:

If the object already has a file associated (open), the function fails. On failure, the failbit flag is set (which can be checked with member fail)and depending on the value set with exceptions an exception may be thrown.

If you have opened a file, close it before opening.

Edit: As shown above you can check which 'failbit flag is set by using stream.rdstate()

OGH
  • 540
  • 1
  • 4
  • 15
1

Having just found the same situation, I resolved a typo that had read

if (!outfile.is_open());

{

}

not noticing the semi-colon after the if statment...

jteez
  • 13
  • 5
0
io_state word=ofstream.rdstate();
if(word & ios::failbit){
    cout<<"Failbit flag is set";
}
//etc
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

Quoting this

std::string DescribeIosFailure(const std::ios& stream)
{
  std::string result;

  if (stream.eof()) {
    result = "Unexpected end of file.";
  }

#ifdef WIN32
  // GetLastError() gives more details than errno.
  else if (GetLastError() != 0) {
    result = FormatSystemMessage(GetLastError());
  }
#endif

  else if (errno) {
#if defined(__unix__)
    // We use strerror_r because it's threadsafe.
    // GNU's strerror_r returns a string and may ignore buffer completely.
    char buffer[255];
    result = std::string(strerror_r(errno, buffer, sizeof(buffer)));
#else
    result = std::string(strerror(errno));
#endif
  }

  else {
    result = "Unknown file error.";
  }

  boost::trim_right(result);  // from Boost String Algorithms library
  return result;
}
Community
  • 1
  • 1
zangw
  • 43,869
  • 19
  • 177
  • 214