2012-07-13 80 views
0

我有一個存儲在文本文件中的文件列表。我一行一行地讀取文件,並將它們存儲在一個字符串數組中。文件列表如下所示:如何從文本文件中讀取文件名列表並在C++中打開它們?

04_02_1310.csv 
04_03_1350.csv 
04_04_0421.csv 
04_05_0447.csv 

等等。讓我們把我的字符串數組

filelist[i] 

假設我想在列表中打開的第一個文件:

inputFile.open(filelist[0].c_str()); // This cannot open file 

文件無法打開。如果我用引號將文件名,一切順利罰款:

inputFile.open("04_02_1310.csv"); // This works perfectly 

如果我打印的文件列表中的內容[I],然後正常工作,以及:

cout << filelist[0] << endl; // This outputs 04_02_1310.csv on screen. 

有人可以告訴我上面的方法有什麼問題?這使我在過去的2天內瘋狂,而我正在手動輸入所有內容,以便完成它(100個以上的文件)。

我也願意以其他任何方式來完成這個簡單的任務。

謝謝!

編輯:再次

Data file could not be opened 

感謝:我添加的代碼的相關部分,如果你想看看它是如何實現的:

#include <cstdlib> 
#include <iostream> 
#include <time.h> 
#include <string> 
#include <sstream> 
#include <fstream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 

using namespace std; 

//Declarations for I/O files 
ifstream inputFile; 

//Declare other variables (forgot to add these in my previous EDIT, sorry) 
int number_of_files; 
string line; 
string *filelist = NULL; 

//Open file list and count number of files 
inputFile.clear(); 
inputFile.open("filelist.txt", ios::in); 

//exit and prompt error message if file could not be opened 
if (!inputFile){ 
    cerr << "File list could not be opened" << endl; 
    exit(1); 
}// end if 

// count number of lines in the data file and prompt on screen 
number_of_files = 0; 

while (getline(inputFile, line))   
    number_of_files++; 

cout << "Number of files to be analyzed: " << number_of_files << endl; 

filelist = new string[number_of_files]; 
inputFile.close(); 

//Re-open file list and store filenames in a string array 
inputFile.clear(); 
inputFile.open("filelist.txt", ios::in); 

//exit and prompt error message if file could not be opened 
if (!inputFile){ 
    cerr << "File list could not be opened" << endl; 
    exit(1); 
}// end if 

// store filenames 
i = 0; 
while (getline(inputFile, line)){ 
    filelist[i] = line; 
    //cout << filelist[i] << endl; 
    i = i + 1; 
}   

inputFile.close(); 

//open first file in the list, I deleted the loop to focus on the first element for now 

inputFile.clear(); 
inputFile.open(filelist[0].c_str(), ios::in); 

//exit and prompt error message if file could not be opened 
if (!inputFile){ 
    cerr << "Data file could not be opened" << endl; 
    exit(1); 
}// end if 

的輸出!

+2

什麼'COUT <<文件列表[0] .c_str()<< ENDL;'或'COUT <<文件列表[I] .c_str()<< ENDL;'? – 2012-07-13 15:58:50

+4

你可以發佈循環? – pearcoding 2012-07-13 15:59:22

+0

Luchian'cout << filelist [0] .c_str()<< endl;'給出了與'cout << filelist [0] << endl;'完全相同的結果。這使我瘋狂:) – marillion 2012-07-13 16:05:26

回答

2

可能您的文本文件中仍然存在'\ n'字符(或EOF,'\ 0'),您應該嘗試檢查字符串是否爲「乾淨」。

+0

這似乎是對我來說最可能的答案 - 隱藏的空白。 – Puppy 2012-07-13 16:33:46

+0

這就是問題所在!感謝您的幫助! – marillion 2012-07-13 16:57:23

0

我建議使用更多的「C++ - 雜交」的做法,雖然從張貼血的優雅的解決方案不同:

int main() 
{ 
    std::vector<std::string> fileList; 

    // ... 

    std::ifstream inputFile("filelist.txt"); 

    // ... 

    std::string line; 
    while(inputFile >> line) 
     filelist.push_back(line); 

    inputFile.close(); 

    // ... 

    for(size_t t = 0; t < filelist.size(); t++) 
    { 
     std::ifstream dataFile(filelist[t].c_str()); 

     // ... 

     dataFile.close(); 
    } 
} 
+0

剛剛嘗試過這種方法,它也可以正常工作。謝謝! – marillion 2012-07-13 17:00:44

2

我也開放給任何其他方式做到這一點簡單的任務。

#include <cstdlib> 
#include <iostream> 
#include <time.h> 
#include <string> 
#include <sstream> 
#include <fstream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 

using namespace std; 

int main() { 

    std::ifstream inputFile("filelist.txt"); 
    std::vector<std::string> fileList; 
    std::string line; 

    if(!inputFile) { 
    std::cerr << "File list could not be opened\n"; 
    return 1; 
    } 

    while(std::getline(inputFile, line)) { 
    fileList.push_back(line); 
    } 

    std::cout << "Number of files to be analyzed: " << fileList.size() << "\n"; 

    std::vector<std::string>::const_iterator it(fileList.begin()); 
    std::vector<std::string>::const_iterator end(fileList.end()); 
    for(;it != end;++it) { 
    std::ifstream inputTxt(it->c_str()); 

    if(!inputTxt) { 
     std::cerr << "Data file could not be opened:" << *it << "\n"; 
     return 1; 
    } 
    while(std::getline(inputTxt, line)) { 
     std::cout << line << "\n"; 
    } 
    } 
} 
+0

也試過這個,它也很完美。非常感謝! – marillion 2012-07-13 17:02:57

+0

驚豔!謝謝你! – IonOne 2014-07-31 10:56:22

相關問題