根据手册页fclose(3)
:
返回值
成功完成后返回 0。否则,
EOF
返回并设置全局变量errno
以指示错误。在任何一种情况下,对流的任何进一步访问(包括对 的另一个调用fclose()
)都会导致未定义的行为。错误
EBADF
底层文件描述符fp
无效。该
fclose()
函数也可能失败并设置errno
为例程指定的任何错误close(2)
,write(2)
或fflush(3)
。
当然fclose(NULL)
应该失败,但我希望它errno
正常返回,而不是直接因分段错误而死亡。这种行为有什么原因吗?
提前致谢。
更新:我将把我的代码放在这里(我正在尝试strerror()
,特别是)。
FILE *not_exist = NULL;
not_exist = fopen("nonexist", "r");
if(not_exist == NULL){
printError(errno);
}
if(fclose(not_exist) == EOF){
printError(errno);
}