Ramadheer Singh
Ramadheer Singh

Reputation: 4234

Why ofstream would fail to open the file in C++? Reasons?

I am trying to open an output file which I am sure has a unique name but it fails once in a while. I could not find any information for what reasons the ofstream constructor would fail.

EDIT: It starts failing at some point of time and after that it continuously fails until I stop the running program which write this file.

EDIT: once in a while = 22-24 hours

code snippet ( I don't this would help but still someone asked for it )

ofstream theFile( sLocalFile.c_str(), ios::binary | ios::out );
if ( theFile.fail() ) 
{
    std::string sErr = " failed to open ";
    sErr += sLocalFile;
    log_message( sErr );
    return FILE_OPEN_FAILED;
}

Upvotes: 3

Views: 7906

Answers (3)

Josh Kelley
Josh Kelley

Reputation: 58352

Too many file handles open? Out of space? Access denied? Intermittent network drive problem? File already exists? File locked? It's awfully hard to say without more details. Edit: Based on the extra details you gave, it sounds like you might be leaking file handles (opening files and failing to close them and so running out of a per-process file handle limit).

I assume that you're familiar with using the exceptions method to control whether iostream failures are communicated as exceptions or as status flags.

In my experience, the iostream classes give very little details on what went wrong when they fail during an I/O operation. However, because they're generally implemented using lower-level Standard C and OS API functions, you can often get at the underlying C or OS error code for more details. I've had good luck using the following function to do 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;
}

Upvotes: 11

T.E.D.
T.E.D.

Reputation: 44804

One possibility is that you have another instance of the same program running.

Another is that perhaps you run two instances (for debugging purposes?) right after each other, and the OS hasn't finished closing the file and resetting the locks before your next instance of the program comes along and asks for it.

Upvotes: 1

Stephen Newell
Stephen Newell

Reputation: 7838

You could be out of space, or there could be a permission issue. The OS may have locked the file as well. Try a different name/path for kicks and see if it works then.

Upvotes: 2

Related Questions