我正在編輯現有的C++代碼,以便使用stringsteam打開多個文件。我有一個從1到7的整數的循環,並且有7個文件需要打開。這些文件被命名爲PMAP1.txt ... PMAP7.txt。我試圖打開它這種方式:C++:無法使用stringstream操作打開文件
ifstream precipfile;
int j = 0;
stringstream ss;
string FileName;
for(j=1;j<6;j++){
ss <<"PMap" << j <<".txt" << endl;
FileName = ss.str();
precipfile.open(FileName.c_str(),ios::in);
if(!precipfile.good())
ReportFatalError("Unable to find or open precipfile");
}
這並不適用於某些reason.It返回「無法找到或打開precipfile」工作。但是,如果我 打開一個文件只需使用一個文件名直接就works.Like:
string FileName = ("PMap.txt");
precipfile.open(FileName.c_str());
這works.Please幫助!
每次打印'ss.str()'。 – chris