我正在编辑现有的 C++ 代码,以便它使用 stringsteam 打开多个文件。我有一个整数从 1 到 7 的循环,我需要打开 7 个文件。这些文件被命名为 PMAP1.txt ... PMAP7.txt。我正在尝试以这种方式打开它:
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" );
}
由于某种原因,这不起作用。它返回“无法找到或打开 precipfile”。但是,如果我直接使用一个文件名打开一个文件,它就可以工作。像:
string FileName = ( "PMap.txt" );
precipfile.open(FileName.c_str());
这行得通。请帮忙!