How to check for the existence of a C file?

try { if ((fopen_s(&f, filename, "r") != NULL) { //так не получается cout << "File is open" << endl; fclose(f); } else throw (FileOpenError(filename, "file wasn't open!")); } catch (FileOpenError& ex) { cout << ex.what(); } 
  • Does religion allow using the stat function? ... - Harry
  • Atomic or not? If atomically, then you need to open, if not, then you can stat() . - 0andriy
  • @Harry, stat() not atomic. - 0andriy
  • Everything, I solved the problem)) with the help of errno_t - anonimys
  • 3
    And how did the tags c , ооп and исключения ооп out to be next? - PinkTux

1 answer 1

problem solved with errno_t

 errno_t err; FILE *f; char* filename = "my.txt"; try { err = fopen_s(&f, filename, "r"); if(err == 0){ cout << "File is open" << endl; fclose(f); } else throw (FileOpenError(filename, "file wasn't open!")); } catch (FileOpenError& ex) { cout << ex.what(); }