Do we need to reset
errno
to zero before calling a function? See below code. Now the scenario isa_dest_path
is an existing directory. But when I execute the code, it always tries tomkdir
but returns error says that the directory can't be created because it exists. In GDB, I checkerrno
before callingopendir()
anderrno
is 2. And it seemserrno
is not set to zero during callingopendir()
. So do I need to reseterrno
to zero before callingopendir()
?errno
may be changed insystem()
calls, then in myelse if
branch I check the result fromsystem()
but notopendir()
. So afteropendir()
, do I need to assignerrno
to a variable then check this variable in theif..elseif..else
branch?
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);
}