創建一個目錄之前,我寫了這段代碼,以檢查是否在Windows和Unix存在的目錄,但我不知道它是否是正確的:檢查,如果是Windows或UNIX在C++
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。有人能給我一些關於這個問題的意見嗎? 謝謝
你不確定哪一點? – hmjd 2012-07-05 15:09:43
@hmjd #ifndef部分...我不確定它是否可以處理這兩種情況(Windows和Unix) – cybertextron 2012-07-05 15:11:52
您是否嘗試過使用boost.filesystem? – PlasmaHH 2012-07-05 15:12:37