2017-12-22 11 views
1

을 유니 코드 파일 이름을 갖는 디렉토리의 크기를 취득하는 것은여기 부스트 : : 파일 시스템을 사용하여 내 알고리즘, 내가 디렉토리의 크기 (바이트)를 계산할 C++

#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; 
} 

그것은 잘 작동 (유니 코드 문자없이, 즉) 단순한 파일 이름으로 파일을하지만, 유니 코드 문자를 가진 파일을 찾을 때 예외가 발생합니다 :

무엇을() :

boost::filesystem::file_size: The filename, directory name, or volume label syntax is incorrect 

무엇을해야 나는한다?

+0

입니다 [튜토리얼] (http://www.boost.org/doc/libs/1_66_0/ libs/filesystem/doc/tutorial.html # Class-path-Constructors),하지만 어떻게 든 명시 적으로 작동하도록'std :: wstring'을 사용해야 할 필요가 있다고 생각합니다. 나는 시도하지 않았고 완전히 잘못 될 수 있습니다 ... – sjm324

+2

여기에서'.string()'을 제거해보십시오. 'handler (e.path(). string());' – balki

답변

0

나는 내 문제를 해결했습니다. 내가 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; 
} 
관련 문제