c++ - When will ofstream::open fail? -


i trying out try, catch, throw statements in c++ file handling, , have written dummy code catch errors. question in order check if have got these right, need error occur. can check infile.fail() not creating file of required name in directory. how able check same outfile.fail() (outfile ofstream infile ifstream). in case, value outfile.fail() true?

sample code [from comments on unapersson's answer, simplified make issue clearer -zack]:

#include <fstream> using std::ofstream;  int main()  {     ofstream outfile;     outfile.open("test.txt");     if (outfile.fail())          // something......      else          // else.....      return 0;  } 

the open(2) man page on linux has 30 conditions. intresting ones are:

  • if file exists , don't have permission write it.
  • if file doesn't exist, , don't have permission (on diretory) create it.
  • if don't have search permission on parent directory.
  • if pass in bogus char* filename.
  • if, while opening device file, press ctrl-c.
  • if kernel encountered many symbolic links while resolving name.
  • if try open directory writing.
  • if pathname long.
  • if process has many files open already.
  • if system has many files open already.
  • if pathname refers device file, , there no such device in system.
  • if kernel has run out of memory.
  • if filesystem full.
  • if component of pathname not directory.
  • if file on read-only filesystem.
  • if file executable file being executed.

Comments