有没有办法使用带有unicode文件路径的boost :: filesystem :: path?特别是我想用std :: wstring代替std :: string.
我正在使用Windows平台,我有时需要处理一个包含unicode char的文件路径.
我编写了以下例程,以便将目录中的所有文件复制到子目录然后将其删除,但我仍然在copy_fail上获得访问被拒绝,这看起来很容易让我误解.路径是更正,文件存在,并且权限在刚刚创建的目标目录中不是只读的.
有什么建议如何寻找问题的根源?
我试图调试,但我没有boost :: filesystem源代码.
任何建议表示赞赏.
void
moveConfigurationFileToSubDirectory()
{
// TODO: Catch errors.
boost::filesystem::path full_path( boost::filesystem::current_path() );
// Create directory subdir if not exist
boost::filesystem::path subdirPath(kSubdirectory);
if ( !boost::filesystem::exists(subdirPath) )
{
PLog::DEV.Development(devVerbose, "%s: creating directory %s", __FUNCTION__, subdirPath.string());
boost::filesystem::create_directories(subdirPath);
} else
PLog::DEV.Development(devVerbose, "%s: directory %s exist", __FUNCTION__, subdirPath.string());
// Iterate through the configuration files defined in the static array
// copy all files with overwrite flag, if successfully delete file (looks like there is not remove)
for (int i = …Run Code Online (Sandbox Code Playgroud) 由于类recursive_directory_iterator(我不必编写递归代码),我正在使用new boost,v1.5.3来执行此任务,如下所示:
void ListDirRec(const char *Dir, vector<string>& DirFileList, const char* ext)
{
recursive_directory_iterator rdi(Dir);
recursive_directory_iterator end_rdi;
DirFileList.empty();
string ext_str0(ext);
for (; rdi != end_rdi; rdi++)
{
rdi++;
//cout << (*di).path().string() << endl;
cout << (*rdi).path().string() << endl;
//cout << " <----- " << (*rdi).path().extension() << endl;
//string ext_str1 = (*rdi).path().extension().string();
if (ext_str0.compare((*rdi).path().extension().string()) == 0)
{
DirFileList.push_back((*rdi).path().string());
}
}
Run Code Online (Sandbox Code Playgroud)
具有特定扩展名的功能列表文件.此函数适用于案例,但经常返回"断言失败错误",如:
**** Internal program error - .... assertion (m_imp.get()) ... operations.hpp(952): dereference of end recursive_directory_iterator
Run Code Online (Sandbox Code Playgroud)
我几乎找不到这个错误的原因.可以尝试..抓住帮助吗?提前感谢您的帮助
当我boost::filesystem用来获取目录中的文件名列表时,我会收到文件名和目录名:
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main()
{
path p("D:/AnyFolder");
for (auto i = directory_iterator(p); i != directory_iterator(); i++)
{
cout << i->path().filename().string() << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
file1.txt
file2.dat
Folder1 //which is a folder
Run Code Online (Sandbox Code Playgroud)
有没有快速的方法来区分文件和文件夹?我的操作系统是Windows 8.1,如果重要的话.
我需要将绝对路径A与路径B结合起来,因为B既可以是相对的也可以是绝对的,最好使用boost :: filesystem。
换句话说,我想拥有:
/usr/home/+ abc=/usr/home/abc/usr/home/+ ../abc= /usr/home/../abc(或者甚至更好/usr/abc- 这不是我的问题)/usr/home/+ /abc=/abc前两个对于/操作员来说很容易,但是我无法使第三个工作。
我试过了:
std::cout << boost::filesystem::path("/usr/home/") / "/abc";
Run Code Online (Sandbox Code Playgroud)
印刷品/usr/home//abc。
std::cout << boost::filesystem::path("/usr/home/") + "/abc";
Run Code Online (Sandbox Code Playgroud)
仍然打印/usr/home//abc。
当然,通过查看路径B并使用它,我可以“看到”路径B是绝对路径,但我不想硬编码前导的检查,/因为在Windows上路径可以不同(例如C:\\或\\)。
我正在使用boost::filesystem创建一个空文件夹(在 Windows 中)。假设我要创建的文件夹的名称是New Folder。当我运行以下程序时,会按预期创建一个具有所需名称的新文件夹。第二次运行程序时,我希望创建新文件夹 (2)。虽然这是一个不合理的期望,但这就是我想要实现的。有人可以指导我吗?
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path dstFolder = "New Folder";
boost::filesystem::create_directory(dstFolder);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期输出:
刚开始学习boost :: filesystem.
谢谢
所以我有一个path p我可以调用的例子is_regular_file(p),file_size(p)但是如何将该文件读入stringstream?(顺便说一句我只需阅读它)
我想使用boost :: filesystem获取目录中所有文件的列表
我能够使用打印文件名,cout但我无法将文件名存储在字符串变量中.我也尝试过类型转换和strcpy,但没有一种方法可行.
以下是代码:
char dir[100] = "/home/harsh/";
namespace fs = boost::filesystem;
fs::directory_iterator start = fs::directory_iterator(dir);
fs::directory_iterator di = start;
for (; di != fs::directory_iterator(); ++di)
{
std::cout << "hello .. " << di->path() << std::endl;
//std::string strHarsh = di->path(); //Error
}
Run Code Online (Sandbox Code Playgroud) 为什么以下代码中存在悬空引用?我认为对const的引用总是将临时的寿命延长到它们的范围.
boost::filesystem::recursive_directory_iterator it(dir_name);
const std::string& extension = it->path().extension().string();
std::cout << extension << std::endl; // error, dangling reference
Run Code Online (Sandbox Code Playgroud) 我有这种代码和平
string targetFile = string + boost::filesystem::path.filename()
Run Code Online (Sandbox Code Playgroud)
问题是它认为path.filename()是路径而不是文档告诉的字符串(http://www.boost.org/doc/libs/1_36_0/libs/filesystem/doc/reference.html#Class-template-basic_path)
首先,我使用Visual Studio 2015实现的文件系统库来自即将推出的基于Boost :: Filesystem的C++ 17标准.
基本上,我要做的是保存文件的时间戳(它是"最后写入时间"),将该文件的内容与所述时间戳一起复制到存档中,然后将该文件提取出来并使用保存的时间戳恢复正确"最后写作时间".
// Get the file's 'last write time' and convert it into a usable integer.
__int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time(src)).time_since_epoch().count();
// ... (do a bunch of stuff in here)
// Save the file
ofstream destfile(dest, ios::binary | ios::trunc);
destfile.write(ptr, size);
// Correct the file's 'last write time'
fs::last_write_time(dest, chrono::time_point<chrono::system_clock>(chrono::seconds(timestamp)));
Run Code Online (Sandbox Code Playgroud)
问题是新文件总是以一个时间戳等于它创建的时间(现在),因为它根本就没有调用last_write_time()过.
当我尝试将时间戳从一个现有文件复制到另一个文件时,它工作正常.当我从文件中复制时间戳时,然后使用fs::copy创建该文件的新副本,然后立即更改副本的时间戳,它也可以正常工作.以下代码正常工作:
// Get the file's 'last write time' and convert it into a usable integer.
__int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time("test.txt")).time_since_epoch().count();
fs::copy("test.txt", "new.txt"); …Run Code Online (Sandbox Code Playgroud) 我想检查用户输入的路径.输入必须提供不存在的文件的路径.此外,如果输入还提供目录,则这些目录必须存在.
示例输入:
/existent_dir_a/existent_dir_b/existent_file.bin < - false
/existent_dir_a/existent_dir_b/non_existent_file.bin < - 好的
/non_existent_dir_a/non_existent_file.bin < - false
non_existent_file.bin < - 好的
existent_file.bin < - false
下面的代码显示了一个示例.我希望它能实现我的目标,但我还有一个问题.
我怎么能摆脱output.parent_path().string().size() != 0它,因为它对我来说似乎有点难看?
#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
#include <exception>
namespace fs = boost::filesystem;
int main(int ac, char ** av)
{
fs::path output(av[1]);
std::cout << output << std::endl;
std::cout << output.parent_path() << std::endl;
if (fs::exists(output))
{
std::string msg = output.string() + " already exists";
throw std::invalid_argument(msg);
}
if ( output.parent_path().string().size() != 0 &&
!fs::exists(output.parent_path()) …Run Code Online (Sandbox Code Playgroud)