我需要修改文件的"已创建"(如果存在),"已修改"和"已访问"时间戳.理想情况下,这将是一个独立于平台的解决方案.
我查看了boost库,但我看不到任何相关内容.我发现它相关的东西最近是这适用于Windows.
有人可以帮忙吗?谢谢.
我知道这听起来很愚蠢,但看看这个简单的例子(工作目录应该有多个项目):
#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>
#include <cassert>
int main()
{
using namespace boost::filesystem;
directory_iterator it("./");
directory_iterator it_copy = it;
++it;
assert(it_copy != it);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
it_copy与...一起修改it!(提升1.45)哪些因素可能导致这种设计(directory_iterator类似于智能ptr)?
我只需要保存一份副本directory_iterator以便以后使用它.
我试图使用Boost.Filesystem库迭代目录.
问题是,当我尝试实例化一个路径对象时,我得到一个std :: length_error,消息"string too long",包含任意长度的字符串,即使例如"pippo".
我已经尝试了所有这些:
string s = "pippo";
path p(s);
path p(s.begin(), s.end());
path p(s.c_str());
path p("pippo");
Run Code Online (Sandbox Code Playgroud)
我在Windows 7上使用boost预编译版本1.47 for vc ++ 10.
谢谢,卢卡
编辑
这是执行的增强代码(path.hpp第129行)
template <class Source>
path(Source const& source,
typename boost::enable_if<path_traits::is_pathable<
typename boost::decay<Source>::type> >::type* =0)
{
path_traits::dispatch(source, m_pathname, codecvt());
}
Run Code Online (Sandbox Code Playgroud)
并从(path_traits.hpp第174行)抛出错误
template <class U> inline
void dispatch(const std::string& c, U& to, const codecvt_type& cvt)
{
if (c.size())
convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
}
Run Code Online (Sandbox Code Playgroud)
抛出的函数是"转换".从调试器我看到了两者
&*c.begin()
Run Code Online (Sandbox Code Playgroud)
和
&*c.begin() + c.size()
Run Code Online (Sandbox Code Playgroud)
正确执行
可能重复:
使用boost获取文件的所有者和组
我想使用boost :: filesystem来确定特定用户拥有哪些文件和目录/文件夹.
我需要在Linux(ext3)和Windows(NTFS)文件系统上执行此操作.
如何使用boost :: filesystem获取指定路径的用户标识符?
提前致谢.
编辑:从所选答案派生的代码:
#include <iostream>
#include <string>
#ifdef LINUX
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#else
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include "accctrl.h"
#include "aclapi.h"
#pragma comment(lib, "advapi32.lib")
#endif
bool getOwner(const std::string &Filename, std::string &Owner)
{
#ifdef LINUX
struct stat FileInfo;
stat(Filename.c_str(), &FileInfo);
struct passwd *UserDatabaseEntry = getpwuid(FileInfo.st_uid);
struct group *GroupDatabaseEntry = getgrgid(FileInfo.st_gid);
//
if (UserDatabaseEntry != 0)
{
Owner = UserDatabaseEntry->pw_name;
return true;
}
else
{
return false;
}
#else …Run Code Online (Sandbox Code Playgroud) 我有一个使用在Windows XP SP3中运行的Boost 1.47.0的Visual Studio 2008 C++ 03应用程序.
该调用boost::filesystem::create_directories( L"c:\\foo\\bar" );抛出std::bad_alloc异常.
在输出窗口中,我看到一个调试堆消息:"HEAP [test.exe]:无效的分配大小 - CDCDCDCE(超过7ffdefff)"
callstack显示boost.filesystem在Microsoft标准库文件xlocale第309行中创建新的语言环境和最后一行可见代码.
msvcp90.dll!std::_Allocate<char>() + 0x17 bytes
msvcp90.dll!std::allocator<char>::allocate() + 0xf bytes
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy() + 0x70 bytes
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow() + 0x26 bytes
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign() + 0x50 bytes
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> >() + 0x24 bytes
msvcp90.dll!std::locale::_Locimp::_Locimp() + 0x47 bytes
> test.exe!std::locale::locale<windows_file_codecvt>(const std::locale & _Loc={...}, const windows_file_codecvt * _Facptr=0x00b48f60) Line 309 + 0x69 bytes C++
test.exe!`anonymous namespace'::default_locale() Line 735 C++
test.exe!`anonymous namespace'::path_locale() Line 777 + 0x2a bytes …Run Code Online (Sandbox Code Playgroud) 使用boost_filesystem时,Boost会不断为文件名添加引号.
Foo.cpp中:
#include <iostream>
#include <boost/filesystem.hpp>
int main( int argc, char * argv[] )
{
std::cout << argv[0] << std::endl;
boost::filesystem::path p( argv[0] );
std::cout << p << std::endl;
std::cout << p.filename() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
g++ foo.cpp -o foo -lboost_filesystem -lboost_system
Run Code Online (Sandbox Code Playgroud)
输出:
./foo
"./foo"
"foo"
Run Code Online (Sandbox Code Playgroud)
这有些出乎意料,在我的情况下不方便.这是真的故意,还是我在这方面有点老版本的Boost(1.46.1)车?有什么方法可以避免它们被添加?
我仔细阅读了文档,但除了在示例输出中没有显示引号的教程之外,我没有开悟.
我正在尝试比较两个目录,并找到内部不同的文件.所以我使用a boost::filesystem::recursive_directory_iterator将每个目录的所有内容添加到各自的向量中.然后我按字母顺序对每个矢量进行排序并开始比较路径 问题是它包括'基础'路径,我不希望这样,例如:
版本1 /资产/ info.txt
版本2 /资产/ info.txt
这些路径比较不同,但我希望它们进行比较.为了澄清,我甚至没有检查二进制文件,只是到目前为止的路径名称.我要像这样比较它们:
/Assets/info.txt
我已经浏览了几个小时的提升文档,我确信必须有一个优雅的解决方案来解决我的问题.我意识到我可以通过获得我想要的子串来解决这个问题,但必须有一个不那么难看的方式.
虽然我正在使用C++ 11,但这个问题与提升相关,因为我正在处理错误boost::file_system.
在以下情况中:
try {
// If p2 doesn't exists, canonical throws an exception
// of No_such_file_or_directory
path p = canonical(p2);
// Other code
} catch (filesystem_error& e) {
if (e is the no_such_file_or_directory exception)
custom_message(e);
} // other catchs
}
Run Code Online (Sandbox Code Playgroud)
如果我在抛出所需的异常(no_such_file_or_directory)时打印错误值:
// ...
} catch (filesystem_error& e) {
cout << "Value: " << e.code().value() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到了价值2.它的价值是相同的e.code().default_error_condition().value().
我的问题是:不同错误类别的不同错误条件是否具有相同的值?我的意思是,我是否需要同时检查错误类别和错误值,以确保我收到特定错误?在这种情况下,最干净的方法是什么?
假设我有以下代码:
#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>
void foo()
{
const boost::filesystem::wpath& appdata_folder = std::getenv("APPDATA");
while (true)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
}
}
int main()
{
boost::thread first(foo);
boost::thread second(foo);
first.join();
second.join();
}
Run Code Online (Sandbox Code Playgroud)
它在运行时失败,并显示以下错误:
* Internal Program Error - assertion (codecvt_facet_ptr()) failed in const class std::codecvt &__cdecl boost::filesystem::path::codecvt(void): libs\filesystem\src\path.cpp(888): codecvt_facet_ptr() facet hasn't been properly initialized
In the documentation I read that it's not thread-safe in terms of parallel set and get operations, not the several get operations as in my case. _wgetenv function works …
从CodeBlocks和cmd执行时,以下程序给出不同的结果 - :
#include <iostream>
#include <string>
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main()
{
// A valid existing folder path on my system.
// This is actually the path containing the program's exe.
path source = "D:\\anmol\\coding\\c++\\boost\\boost1\\bin\\release";
cout << "output = " << equivalent( source, "D:" ) << " !!!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CodeBlocks从IDE内部运行后的输出 - :
output = 0 !!!
Run Code Online (Sandbox Code Playgroud)
通过boost1在将当前目录更改为包含可执行文件的文件夹(source代码中提到的路径)后执行cmd的输出- :
output = 1 !!!
Run Code Online (Sandbox Code Playgroud)
据我所知,CodeBlocks给出的输出应该是正确的.
我在Windows 7 SP1 …
boost ×10
boost-filesystem ×10
c++ ×10
filesystems ×2
cmd ×1
codeblocks ×1
iterator ×1
system-error ×1
windows ×1