2012-03-14 49 views
0

我需要從文件夾中的一組文件中逐一讀取文件。 是否有可能從C++庫使用ifstream,而不是全文件名,如 ifstream myfile(「file.txt」),我打算使用一些包含每個文件名的字符串變量。我將放入一些新文件的所有文件名稱,我的C++類對象可以讀取這些文件。如何讀取數組由C++中的文件組成

感謝, Vlada

+1

'ifstream的MYFILE(fileName.c_str())'?不知道我明白這個問題。 – BoBTFish 2012-03-14 12:05:32

回答

1

是:

std::ifstream listoffilenames("filenames"); 

std::string name; 
while(std::getline(listoffilenames, name)) // Assume the file contains 1 name per line. 
{ 

    std::ifstream file(name.c_str());  // Open the file name you just read. 
              // Now you can read from it like normal. 
    // READ file 
}