How can I complete the program execution process if the file specified for the input stream does not exist?
- man 2 exit , and even better man 3 err - avp
|
1 answer
Exit the main
function using the return
. File availability can be checked by:
int main() { FILE *fo = fopen("Путь к файлу", "r"); // Файла нет if (fo == 0) { return 1; } ... }
- Technically, an error at this stage means rather that the file cannot be read. It may exist, just be unavailable. Although there are very few tasks where distinguishing these situations is important. - D-side
- @ D-side But how can you tell when it does not exist, and when it is not available? - nikita
- @ nikita is portable - no way. The OS may deliberately provide this illusion. - D-side
- one@ nikita, if we use
fopen
, then we most likely mean that we execute the program inPOSIX
. And there is a global variableerrno
(available via#include <errno.h>
). If it isENOENT
, then there is no such file. (all its relevant values can be found in man 2 open ) - avp - Yet
return 0;
, somehow not good, because 0 is the code for the normal completion of the process. - avp pm
|