errno在调用函数之前我们需要重置为零吗?见下面的代码.现在场景是a_dest_path现有目录.但是当我执行代码时,它总是尝试mkdir但返回错误表示无法创建目录,因为它存在.在GDB中,我errn在调用之前检查o opendir()并且errno是2.并且errno在调用期间似乎没有设置为零opendir().所以我需要errno在呼叫前重置为零opendir()吗?
errno可以在system()通话中更改,然后在我的else if分支中我检查结果system()但不是opendir().那么之后opendir(),我需要分配errno一个变量然后在if..elseif..else分支中检查这个变量吗?
DIR *dp = opendir(a_dest_path.c_str());
if (errno == ENOENT) {
string mkdir_comman = "mkdir " + a_dest_path;
system(mkdir_command.c_str());
} else if (errno == ENOTDIR) {
printf("Destination %s exists but is not directory\n", a_dest_path.c_str());
return k_error_not_directory;
} else if (errno == 0) {
closedir(dp);
}
Run Code Online (Sandbox Code Playgroud)