我的旧Boost代码:
std::string CombinePath(const char* left, const char* right)
{
boost::filesystem::path p = boost::filesystem::complete(
boost::filesystem::path(right, boost::filesystem::native),
boost::filesystem::path(left, boost::filesystem::native) );
return p.string();
}
在新的Boost版本中,这仅使用#define BOOST_FILESYSTEM_VERSION 2.什么是complete新Boost版本的替代品?
出于好奇心,我想知道为什么boost :: filesystem库有一个编译组件,而在其他情况下,没有必要编译组件.编译部分中的什么不能在hpp中?
让我感到困惑的是,在我的计算机上,无论如何我都会编译这部分(在使用库之前),因此我希望每次构建应用程序时都可以执行我在BJam中执行的相同编译.
为什么还有额外的.so / .a文件?
我想对 boost 文件系统函数 create_directories() 的失败情况进行单元测试,即当 create_directory 失败时。有人可以提供有关如何执行此操作的任何建议吗?另一个要求是代码需要是跨平台的。
我正在使用boost-filesystem来搜索具体路径中的所有文件.我还想检索这个文件的创建数据,最后打开和最后更新,所以当我在Windows中工作时,我需要使用GetFileTime(这需要一个我将通过CreateFile函数得到的HANDLE.
关键是通过boost文件系统我得到一个字符串,如
string filename ="C:\ Users\MyUser\Desktop\PDN.pdf";
我需要将此字符串转换为LPCWSTR.
因此我做了几次尝试都失败了,例如:
HANDLE hFile = CreateFile((LPCWSTR)fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)
但是当这样做时,它成功了:
HANDLE hFile = CreateFile(L"C:\\Users\\MyUSer\\Desktop\\PDN.pdf", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何使用字符串变量将字符串解析为PWSTR?如果可能的话(我猜不是),是否有任何函数可以改变原始路径添加斜杠哪里找到另一个斜杠?
非常感谢
编辑:这是我在这里阅读后的方式:
wstring fileFullPathWstring = winAPII.stringToWstring(iter-> path().string());
HANDLE hFile = CreateFile(fileFullPathWstring.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
使用功能:
wstring WinAPIIteraction::stringToWstring(string stringName){
int len;
int slength = (int)stringName.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, stringName.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, stringName.c_str(), slength, buf, len);
std::wstring r(buf); …Run Code Online (Sandbox Code Playgroud) 我正在尝试转换相对路径并将其转换为绝对路径以使用 boost 文件系统传递给 SQLite。这应该适用于 windows 和 linux
boost::filesystem::path path("../../data/dominion");
boost::filesystem::path file("dominion.db");
boost::filesystem::path canonical = boost::filesystem::canonical(dataPath / file);
Run Code Online (Sandbox Code Playgroud)
规范回报
m_pathname=L"D:/Users\\me\\Documents\\tonkatsu\\data\\dominion\\dominion.db"
Run Code Online (Sandbox Code Playgroud)
如您所见,路径“D:/”的开头不正确。我也尝试调用 normalize() 没有成功
有没有办法解决这个问题?
考虑 a boost::filesystem::path p,是否有可能有 boost::filesystem::is_regular_file(p) == true和std::ifstream(p.c_str()).is_open() == false ,是否可以同时如果有,是在什么情况下?
上下文是比较函数的断言的编写:
bool identical_files(const boost::filesystem::path& p1, const boost::filesystem::path& p2)
{
assert(boost::filesystem::is_regular_file(p1));
assert(boost::filesystem::is_regular_file(p2));
std::ifstream f1(p1.c_str());
assert(f1.is_open()); // IS THIS REDUNDANT ???
std::ifstream f2(p2.c_str());
assert(f2.is_open());
// ...
// ...
// ...
}
Run Code Online (Sandbox Code Playgroud) 我有一个 C++ 程序,我需要当前路径来稍后创建一个文件夹。我的可执行文件的位置是,比方说/home/me/foo/bin。这是我运行的:
//Here I expect '/home/me/foo/bin/', but get ''
auto currentPath = boost::filesystem::current_path();
//Here I expect '/home/me/foo/', but get ''
auto parentPath = currentPath.parent_path();
//Here I expect '/home/me/foo/foo2/', but get 'foo2/'
string subFolder = "foo2";
string folderPath = parentPath.string() + "/" + subFolder + "/";
//Here I expect to create '/home/me/foo/foo2/', but get a core dump
boost::filesystem::path boostPath{ folderPath};
boost::filesystem::create_directories( boostPath);
Run Code Online (Sandbox Code Playgroud)
我在 Ubuntu 16.04 上运行,使用安装了包管理器 Conan 的 Boost 1.66。
我曾经在不使用柯南的情况下使用以前版本的 Boost(我相信是 1.45)成功运行它。Boost 只是正常安装在我的机器上。我现在在运行时得到一个核心转储create_directories( boostPath);。
两个问题: …
我正在开发一个 C++ 程序,该程序使用 cmake 和 conan 进行编译,并提升 1.7.4。最近,我开始收到:错误:#error Compiling Filesystem version 3 file with BOOST_FILESYSTEM_VERSION returned!= 3。
该程序直到最近都运行良好,现在才开始出现此错误。
这是我的cmake代码
#find external libraries with Conan
----------------------------------------------------------
conan_check(VERSION 1.0.0 REQUIRED)
message(STATUS "Downloading dependency libraries with Conan")
#The boost dependency is tricky.
#Need 1.74 for correct behavior, and need options to successfully build on mac
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
#workaround for https://github.com/conan-io/conan-center-index/issues/4097
set(CONAN_OPTIONS boost:without_fiber=True boost:without_nowide=True)
else()
set(CONAN_OPTIONS )
endif()
conan_cmake_run(REQUIRES boost/1.74.0 jsoncpp/[>=1.8.4] eigen/[>=3.3.7] cgal/[>=5.1]
OPTIONS
${CONAN_OPTIONS}
BUILD missing
CMAKE_TARGETS
BASIC_SETUP
UPDATE)
Run Code Online (Sandbox Code Playgroud)
我相信 boost 1.7.4 …
我试图将引用变量i_RootPath的值设置为while循环内的不同值,如果单击相应的按钮.编译不喜欢我分配i_RootPath的方式.它说:
没有可行的重载'='
如何从生成的按钮调用的不同方法中成功更改"i_RootPath"的值?
void NodeViewApp::AddToolbar( boost::filesystem::path& i_RootPath ) {
boost::filesystem::path currentPath = i_RootPath;
while( currentPath.has_root_path() ){
WPushButton* currentButton = new WPushButton( "myfile.txt" );
currentButton->clicked().connect( std::bind([=] () {
i_RootPath = currentPath;
}) );
currentPath = currentPath.parent_path();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在将一些代码从VS2010(使用Boost 1.55)迁移到VS 2015(使用Boost 1.60)。
我最终得到“ Microsoft Visual C ++运行时库”的报告,该报告abort() has been called同时引发了异常。但是,我可以毫无问题地抛出其他异常(并且它过去曾与VS2010 / boost1.55一起使用):
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>
int main( int argc, char* argv[] )
{
// Stepping to folder:
try
{
boost::filesystem::current_path("B:/dev/msvc2015/vobs_bci/public/tst/base/cppunit/utlfile");
std::cout << "Worked" << std::endl; // works OK
}
catch (...)
{
}
// test throwing upon copy_directory because dource folder does not exist:
try
{
boost::filesystem::copy_directory("s", "b");
}
catch (...)
{
std::cout << "Caught" << std::endl; // works OK
}
// test …Run Code Online (Sandbox Code Playgroud) boost-filesystem ×10
c++ ×8
boost ×5
conan ×2
c++11 ×1
cmake ×1
path ×1
unit-testing ×1
winapi ×1
wt ×1