我想知道 fopen 是否由于路径长或文件不存在而在 C 中失败
#include<stdio.h>
#include <errno.h>
extern int errno ;
int main(){
FILE *p;
int errnum;
p=fopen("C:\\Users\\kkm\\testtttttttttttttttttttttttttttttttttttttt\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\\ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\\b.txt","w");
//p=fopen("C:\\Users\\kkm\\testtttttttttttttttttttttttttttttttttttttt\\xxxx.txt","r");
if (p == NULL) {
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
} else {
fclose (p);
}
return 0;
}
第一个 fopen 的路径很长,而第二个 fopen 很短但文件不存在。对于这两种情况,输出是相似的:
Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory
有没有办法知道区别?