C++ 使用boost::filesystem将文件路径从windows转换到linux,然后再转换回来
是我,还是boost::filesystem::path::make_首选不将“\”转换为“/ davidan@kempt:~/Documents/prog/work!$/练习//路径\信息c:\pitou foo/bar\baa.txtC++ 使用boost::filesystem将文件路径从windows转换到linux,然后再转换回来,c++,linux,windows,path,boost-filesystem,C++,Linux,Windows,Path,Boost Filesystem,是我,还是boost::filesystem::path::make_首选不将“\”转换为“/ davidan@kempt:~/Documents/prog/work!$/练习//路径\信息c:\pitou foo/bar\baa.txt 组合路径: 最后我们可以这样做: string f = filename; # ifdef BOOST_POSIX_API //workaround for user-input files std::replace(f
组合路径:
最后我们可以这样做:
string f = filename;
# ifdef BOOST_POSIX_API //workaround for user-input files
std::replace(f.begin(), f.end(), '\\', '/');
# endif
必须有一个原因来解释为什么它还没有被处理…?这里很好地解释了它在Linux上没有被处理的原因: 引述: 如果在Linux上执行示例35.5,则返回值为 不一样。大多数成员函数返回空字符串,但 相对路径()和文件名(),返回“C:\Windows\System”。这 表示字符串“C:\Windows\System”被解释为文件 Linux上的名称,这是可以理解的,因为它既不是 路径的可移植编码或平台相关编码 Linux。因此,Boost.Filesystem别无选择,只能解释它 作为文件名
这没有使用
boost
,但我手动实现了两个转换方向
filesystemutils.hpp
:
#pragma once
#include <string>
static const char *const WSL_FILE_PATH_HEADER = "/mnt";
static const char WINDOWS_FILE_PATH_SEPARATOR = '\\';
static const char UNIX_FILE_PATH_SEPARATOR = '/';
std::string windows_to_unix_file_path(std::string file_path, bool is_wsl = true);
std::string unix_to_windows_file_path(std::string file_path, bool is_wsl = true);
std::string windows_path_to_host_operating_system_path(std::string file_path, bool is_wsl = true);
#include "FileSystemPathUtils.hpp"
#include <string>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
std::string windows_to_unix_file_path(std::string file_path, bool is_wsl) {
// Replace the slashes
std::replace(file_path.begin(), file_path.end(), WINDOWS_FILE_PATH_SEPARATOR, UNIX_FILE_PATH_SEPARATOR);
// Convert the drive letter to lowercase
std::transform(file_path.begin(), file_path.begin() + 1, file_path.begin(),
[](unsigned char character) {
return std::tolower(character);
});
// Remove the colon
const auto drive_letter = file_path.substr(0, 1);
const auto remaining_path = file_path.substr(2, file_path.size() - 2);
file_path = drive_letter + remaining_path;
std::stringstream stringstream;
if (is_wsl) {
stringstream << WSL_FILE_PATH_HEADER;
}
stringstream << "/";
stringstream << file_path;
return stringstream.str();
}
std::string unix_to_windows_file_path(std::string file_path, bool is_wsl) {
if (is_wsl) {
file_path = file_path.erase(0, strlen(WSL_FILE_PATH_HEADER));
}
// Delete the leading forward slash
file_path.erase(0, 1);
// Convert the drive letter to uppercase
std::transform(file_path.begin(), file_path.begin() + 1, file_path.begin(),
[](unsigned char character) {
return std::toupper(character);
});
// Replace the slashes
std::replace(file_path.begin(), file_path.end(), UNIX_FILE_PATH_SEPARATOR,
WINDOWS_FILE_PATH_SEPARATOR);
std::stringstream stringstream;
stringstream << file_path.at(0);
stringstream << ":";
stringstream << file_path.substr(1, file_path.size() - 1);
return stringstream.str();
}
std::string windows_path_to_host_operating_system_path(std::string file_path, bool is_wsl) {
#ifdef _WIN32
return file_path;
#else
return windows_to_unix_file_path(std::move(file_path), is_wsl);
#endif
}
您不能使用常见的环境变量,例如
HOME
和getenv
它们吗?包含在path.hpp:>#ifdef BOOST_WINDOWS_API typedef wchar_t value_type;BOOST\u STATIC\u constepr value\u type首选\u分隔符=L'\\';\else typedef char value_type;BOOST_STATIC_CONSTEXPR value_type首选_分隔符='/';#如果是这样的话,这个问题应该已经解决了,不是吗?不是,不是…>>路径&make_preferred();效果:包含的路径名转换为首选的本机格式。[注意:在Windows上,效果是用反斜杠替换斜杠。在POSIX上,没有效果。--结束注意]我最后不得不在ifdef POSIX条件下用std::replace手动完成。我无法使“首选”也起作用。但是,我确实使用path.string()解决了我的问题(请看)。我希望有帮助。