2015-06-14 28 views
1

我想創建一個程序,隨機選擇一個文件夾給定一個基本路徑,然後在新文件夾中隨機選擇要打開的視頻並開始播放。在C++中給出一個路徑的文件列表

我的主要問題是找到給定路徑內的文件數量。有什麼功能可以做這樣的事嗎?還是類似? 我需要什麼樣的標題?

隨機部分是一種容易。解決這個問題後,我想知道我是否能夠在執行程序時啓動視頻,這應該是我程序的最後一步。

在發佈之前,我已經搜索了很多,我知道你可能會認爲它已經在那裏,但我無法找到足夠我想要的東西。

我希望你能幫助我。

+0

執行視頻和你爲什麼選擇C++? – teivaz

+0

你正在使用哪個操作系統?另外,請張貼您嘗試過的代碼以與文件系統進行交互。 –

+0

[使用C++的目錄中的文件數]可能的重複(http://stackoverflow.com/questions/2802188/file-count-in-a-directory-using-c) – smac89

回答

0

您顯然需要修改此功能以使其適用於您。但這是我能夠找到並做出的功能。我認爲它需要windows.h。它所做的是將Bin/Pictures中所有文件的文件名添加到名爲mTextureNames的矢量中。

void Editor::LoadTextureFileNames() 
    { 
     string folder = "../Bin/Pictures/"; 
     char search_path[200]; 
     sprintf(search_path, "%s*.*", folder.c_str()); 
     WIN32_FIND_DATA fd; 
     HANDLE hFind = ::FindFirstFile(search_path, &fd); 
     if(hFind != INVALID_HANDLE_VALUE) { 
      do { 
       // read all (real) files in current folder 
       // , delete '!' read other 2 default folder . and .. 
       if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 
        this->mTextureNames.push_back(fd.cFileName); 
       } 
      }while(::FindNextFile(hFind, &fd)); 
      ::FindClose(hFind); 
     } 
    } 
2

你應該看看boost.filesystem。沒有提升的C++(或另一個庫集,如Qt)的能力非常有限。

an example in doc

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"; 

     copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type 
      ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is 
                  // converted to a path by the 
                  // path stream inserter 
     } 

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

當然你也可以使用directory_iterator裏面的 「for」 循環:

#include <boost/filesystem.hpp> 
#include <boost/range/iterator_range.hpp> 
#include <iostream> 

using namespace boost::filesystem; 

int main(int argc, char *argv[]) 
{ 
    path p(argc > 1? argv[1] : "."); 

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

     for(auto& entry : boost::make_iterator_range(directory_iterator(p), {})) 
      std::cout << entry << "\n"; 
    } 
} 
0

我的主要問題是找到一個路徑中的文件的數量給予。

雖然功能glob called is C,它不應該是一個問題,如果你的C++編譯器與C.兼容您可以隨時將它包裝在C++ :) Man glob介紹如何使用它。 GLOB_ONLYDIR可讓您將結果限制到目錄。

播放視頻的最簡單方法是調用system()和你最喜歡的球員

相關問題