Will errno == ENOENT be a sufficient check to check if file exists in C?
在Windows 8.1上使用PellesC。
我知道这个主题已经通过许多解决方案得到了解决。 我已经阅读了一些解决方案,它们说明了我理解的
在回答问题的最快方式中,我还阅读了有关比赛条件的重要信息。
和什么是检查文件是否存在于C中的最佳方法? (跨平台)。
因此,如果我在C中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <stdio.h> #include <string.h> #include <errno.h> int file_exists(char filename[]) { int err = 0; //copy of errno at specific instance int r = 0; //1 for exists and 0 for not exists FILE *f = NULL; //validate if (filename == NULL) { puts("Error: bad filename."); return 0; } printf("Checking if file %s exists... ", filename); //check errno = 0; f = fopen(filename,"r"); err = errno; if (f == NULL) { switch (errno) { case ENOENT: r = 0; break; default: r = 1; } printf("errno = %d %s ", err, strerror(err)); } else { fclose(f); r = 1; } if (r == 0) { puts("It does not."); } else { puts("It does."); } return r; } |
文件可能不存在,而您又遇到另一个错误,例如
另一方面,
总结一下:如果得到
- 该文件存在,或者
- 它不可能存在
- 或当时可能已经存在
在开放时。如何处理这些其他错误取决于您和所需的逻辑。一种简单的方法是拒绝继续处理,将错误报告给用户。