mkdir Windows vs Linux

Min*_*aik 15 c++ linux windows mkdir

将Linux工具移植到Windows时出现问题.我在Windows系统上使用MinGW.我有一个处理所有输入/输出的类,内部是这一行:

mkdir(strPath.c_str(), 0777); // works on Linux but not on Windows and when it is changed to
_mkdir(strPath.c_str()); // it works on Windows but not on Linux
Run Code Online (Sandbox Code Playgroud)

任何想法我能做什么,以便它适用于两个系统?

gco*_*ard 29

#if defined(_WIN32)
_mkdir(strPath.c_str());
#else 
mkdir(strPath.c_str(), 0777); // notice that 777 is different than 0777
#endif
Run Code Online (Sandbox Code Playgroud)

  • "_WIN32 - 为Win32和Win64的应用程序定义".(参考:http://msdn.microsoft.com/en-us/library/b0084kay.aspx)因此,此处不需要检查_WIN64. (2认同)