c++ linux/windows 文件夾創建函數

linux下文件夾創建跟windows不同,根據宏定義選擇編譯:

#ifdef _WIN32
#include <direct.h>
#include <io.h>
#else  // Linux/Unix
#include <sys/io.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif
#include <string>
#include <iostream>

std::string database_dir = "******/******";

if (access(database_dir.c_str(), 0) < 0)
	{
#ifdef _WIN32
		mkdir(database_dir.c_str());
#else
        if(mkdir(database_dir.c_str(), S_IRWXU)<0)
        {
            LPFACE_LOG(LOG_ERROR, "Fail to create database directory.");
            throw std::exception();
        }
#endif
	}

linux stat.h 關於mkdir的描述:

/* Create a new directory named PATH, with permission bits MODE.  */
extern int mkdir (const char *__path, __mode_t __mode)

//有如下model:
#define	S_IRUSR	__S_IREAD	/* Read by owner.  */
#define	S_IWUSR	__S_IWRITE	/* Write by owner.  */
#define	S_IXUSR	__S_IEXEC	/* Execute by owner.  */
/* Read, write, and execute by owner.  */
#define	S_IRWXU	(__S_IREAD|__S_IWRITE|__S_IEXEC)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章