2011-07-26 5 views
1

Boost FileSystem 3を使用してディレクトリ内のファイルをループしています。ファイル名を別のlibのchar *にキャストする必要があります。残念ながら、私のC++ fooは欠けています。誰も助けてくれる?Boost FileSystem3イテレータをconst charにキャストする*

int main(int argc, char* argv[]) 
    { 
     path p (argv[1]); // p reads clearer than argv[1] in the following code 

     try 
     { 
     if (exists(p)) // does p actually exist? 
     { 
      if (is_regular_file(p))  // is p a regular file? 
      cout << p << " size is " << file_size(p) << '\n'; 

      else if (is_directory(p))  // is p a directory? 
      { 
      cout << p << " is a directory containing:\n"; 

      typedef vector<path> vec;    // store paths, 
      vec v;        // so we can sort them later 

      copy(directory_iterator(p), directory_iterator(), back_inserter(v)); 

      sort(v.begin(), v.end());    // sort, since directory iteration 
                // is not ordered on some file systems 

      for (vec::const_iterator it (v.begin()); it != v.end(); ++it) 
      { 
       cout << " " << *it << '\n'; 
     /****************** stuck here **************************/ 
     // I need to cast *it to a const char* filename 
     /****************** stuck here **************************/ 
      } 
      } 

      else 
      cout << p << " exists, but is neither a regular file nor a directory\n"; 
     } 
     else 
      cout << p << " does not exist\n"; 
     } 

     catch (const filesystem_error& ex) 
     { 
     cout << ex.what() << '\n'; 
     } 

     return 0; 
    } 

答えて

2

あなたはこれにしましたので、タイプpathのオブジェクトを返します*itの式:

const std::string & s = (*it).string(); 
const char *str = s.c_str(); //this is what you want 

それともあなたは、下記のように、他の変換関数を使用したい:

const std::string & string() const; 
std::string native_file_string() const; 
std::string native_directory_string() const; 

使用する方を選択してください。リンクは、おそらく正規の[ブーストファイルシステムのドキュメント]に行くべき

+1

(http://www.boost.org/doc/:それらのそれぞれが返すものにと最初のドキュメントを読みますlibs/1_47_0/libs/filesystem/v3/doc/reference.html#path-native-format-observers)boost.orgで –

+0

本当にありがとう – macarthy

関連する問題