Elp*_*rto 22 c++ linux windows cross-platform
我正在用C++编写一个跨平台兼容的函数,它根据输入文件名创建目录.我需要知道机器是Linux还是Windows并使用适当的正斜杠或反斜杠.对于下面的代码,如果机器是Linux那么isLinux = true.我如何确定操作系统?
bool isLinux;
std::string slash;
std::string directoryName;
if isLinux
slash = "/";
else
slash = "\\";
end
boost::filesystem::create_directory (full_path.native_directory_string() + slash + directoryName);
Run Code Online (Sandbox Code Playgroud)
Art*_*yom 41
使用:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
static const std::string slash="\\";
#else
static const std::string slash="/";
#endif
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你仍然可以安全地在Windows上使用这个斜杠"/",因为Windows完全理解这一点.因此,只需坚持使用"/"斜杠即可解决所有操作系统的问题,即使OpenVMS foo:[bar.bee]test.ext也可以表示为路径/foo/bar/bee/test.ext.
默认情况下,Visual Studio中#defineŞ_WIN32预处理器中的项目设置。
所以你可以使用
// _WIN32 = we're in windows
#ifdef _WIN32
// Windows
#else
// Not windows
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25673 次 |
| 最近记录: |