テキストファイルに保存されているファイルのリストがあります。ファイルを1行ずつ読み取り、文字列配列に格納します。ファイルリストは次のようになります。
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
filelist [i]の内容を印刷すると、それも正常に機能します。
cout << filelist[0] << endl; // This outputs 04_02_1310.csv on screen.
上記のアプローチの何が問題になっているのか誰かに教えてもらえますか?これは私を過去2日間夢中にさせています、そして私はそれを成し遂げるためにすべてを手動で入力することについてです(100以上のファイルを次々に)。
私はまた、この単純なタスクを実行する他の方法を受け入れています。
ありがとう!!!
編集:それがどのように実装されているかを確認したい場合は、コードの関連部分を追加しています:
#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
出力は次のとおりです。
Data file could not be opened
再度、感謝します!