Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果文件指针在 C 中返回 NULL,我如何确定文件是否不存在或访问权限不存在?我正在Linux中编写代码。并且文件无权访问,但文件存在,那么我如何返回文件不存在或文件无权访问的不同状态。
检查errno尝试打开文件后的值:
errno
if (NULL == (fp = fopen("myfile.txt", "r"))) { if (ENOENT != errno) { fprintf(stderr, "Failed to open file: %s\n", strerror(errno)); } else { fprintf(stderr, "file does not exist\n"); } }