我正在用 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);
Use:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
static const std::string slash="\\";
#else
static const std::string slash="/";
#endif
顺便说一句,您仍然可以在 Windows 上安全地使用这个斜杠“/”,因为 Windows 可以完美地理解这一点。因此,只要坚持使用“/”斜线就可以解决所有操作系统的问题,甚至像路径为 OpenVMS 的操作系统一样foo:[bar.bee]test.ext
可以表示为/foo/bar/bee/test.ext
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)