在 C++ 中获取具有 unicode 文件名的目录的大小
Get the size of the directory having unicode filenames in c++
我想计算目录的大小(以字节为单位),这是我使用 boost::filesystem
的算法
#include <boost/filesystem.hpp>
#include <string>
#include <functional>
using namespace boost::filesystem;
using namespace std;
// this will go through the directories and sub directories and when any
// file is detected it call the 'handler' function with the file path
void iterate_files_in_directory(const path& directory,
function<void(const path&)> handler)
{
if(exists(directory))
if(is_directory(directory)){
directory_iterator itr(directory);
for(directory_entry& e: itr){
if (is_regular_file(e.path())){
handler(e.path().string());
}else if (is_directory(e.path())){
iterate_files_in_directory(e.path(),handler);
}
}
}
}
uint64_t get_directory_size(const path& directory){
uint64_t size = 0;
iterate_files_in_directory(directory,[&size](const path& file){
size += file_size(file);
});
return size;
}
当目录包含具有简单文件名(即没有任何 Unicode 字符)的文件时它工作正常,但是当发现任何具有 Unicode 字符的文件时它会抛出异常:
什么():
boost::filesystem::file_size: The filename, directory name, or volume label syntax is incorrect
我该怎么办?
我的问题已经解决了。我已经删除了 handler(e.path().string())
中的 .string()
,这就是我的新代码
#include <boost/filesystem.hpp>
#include <string>
#include <functional>
using namespace boost::filesystem;
using namespace std;
// this will go through the directories and sub directories and when any
// file is detected it call the 'handler' function with the file path
void iterate_files_in_directory(const path& directory,
function<void(const path&)> handler)
{
if(exists(directory))
if(is_directory(directory)){
directory_iterator itr(directory);
for(directory_entry& e: itr){
if (is_regular_file(e.path())){
handler(e.path()); // here was the bug in the previous code
}else if (is_directory(e.path())){
iterate_files_in_directory(e.path(),handler);
}
}
}
}
uint64_t get_directory_size(const path& directory){
uint64_t size = 0;
iterate_files_in_directory(directory,[&size](const path& file){
size += file_size(file);
});
return size;
}
我想计算目录的大小(以字节为单位),这是我使用 boost::filesystem
的算法#include <boost/filesystem.hpp>
#include <string>
#include <functional>
using namespace boost::filesystem;
using namespace std;
// this will go through the directories and sub directories and when any
// file is detected it call the 'handler' function with the file path
void iterate_files_in_directory(const path& directory,
function<void(const path&)> handler)
{
if(exists(directory))
if(is_directory(directory)){
directory_iterator itr(directory);
for(directory_entry& e: itr){
if (is_regular_file(e.path())){
handler(e.path().string());
}else if (is_directory(e.path())){
iterate_files_in_directory(e.path(),handler);
}
}
}
}
uint64_t get_directory_size(const path& directory){
uint64_t size = 0;
iterate_files_in_directory(directory,[&size](const path& file){
size += file_size(file);
});
return size;
}
当目录包含具有简单文件名(即没有任何 Unicode 字符)的文件时它工作正常,但是当发现任何具有 Unicode 字符的文件时它会抛出异常:
什么():
boost::filesystem::file_size: The filename, directory name, or volume label syntax is incorrect
我该怎么办?
我的问题已经解决了。我已经删除了 handler(e.path().string())
中的 .string()
,这就是我的新代码
#include <boost/filesystem.hpp>
#include <string>
#include <functional>
using namespace boost::filesystem;
using namespace std;
// this will go through the directories and sub directories and when any
// file is detected it call the 'handler' function with the file path
void iterate_files_in_directory(const path& directory,
function<void(const path&)> handler)
{
if(exists(directory))
if(is_directory(directory)){
directory_iterator itr(directory);
for(directory_entry& e: itr){
if (is_regular_file(e.path())){
handler(e.path()); // here was the bug in the previous code
}else if (is_directory(e.path())){
iterate_files_in_directory(e.path(),handler);
}
}
}
}
uint64_t get_directory_size(const path& directory){
uint64_t size = 0;
iterate_files_in_directory(directory,[&size](const path& file){
size += file_size(file);
});
return size;
}