我写了这段代码来检查一个目录是否在 Windows 和 Unix 中都存在,但我不确定它是否正确:
int writeFiles(std::string location)
{
// USED TO FILE SYSTEM OPERATION
struct stat st;
// DEFINE THE mode WHICH THE FILE WILL BE CREATED
const char * mode = "w+b";
/* local curl variable */
// CHECK IF THE DIRECTORY TO WHERE THE FILE ARE GOING EXIST
// IF NOT, CREATE IT
if(stat(location.c_str(), &st) != 0){
#ifndef (defined _WIN32 || defined __WIN64) /* WIN32 SYSTEM */
if (!CreateDirectory(location.c_str(), NULL)){
std::string msg("The location directory did not exists, can't be created\n");
throw std::runtime_error(msg);
}
#elif defined __unix__ /* in the case of unix system */
if(mkdir(location.c_str(), S_IRWXU) != 0){
std::string msg("The dest_loc directory did not exist, can't be created\n");
throw std::runtime_error(msg);
}
#endif
... more code down here.
location
是应该复制文件的路径。但是,在开始复制文件之前,我必须检查目录是否存在,无论是 Windows 还是 Linux。有人可以就这个问题给我一些意见吗?谢谢