2017-12-22 19 views
1

ich die Größe berechnet werden soll (in Bytes) von einem Verzeichnis, hier mein Algorithmus boost ist :: filesystemErmittelt die Größe des Verzeichnisses, Unicode-Dateinamen in 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; 
} 

Es funktioniert gut, wenn Verzeichnis enthält Datei mit einem einfachen Dateinamen (dh ohne Unicode-Zeichen), aber wenn eine Datei mit einem Unicode-Zeichen gefunden wirft es eine Ausnahme:

was():

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

Was sollte Ich mache?

+0

Ich weiß nicht, ob Sie [dieses Tutorial] gesehen haben (http://www.boost.org/doc/libs/1_66_0/ libs/filesystem/doc/tutorial.html # Klassenpfad-Konstruktoren), aber ich denke, Sie müssen irgendwie 'std :: wstring' verwenden, damit es funktioniert? Ich habe es nicht versucht, und das könnte völlig falsch sein ... – sjm324

+2

Versuchen Sie '.string()' hier zu entfernen. 'handler (e.path(). string());' – balki

Antwort

0

Ich habe mein Problem gelöst. Ich habe entfernt .string() in handler(e.path().string()) und das ist alles, hier ist mein neuer Code

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