C++ 尝试在c+;中打开多个文件时出错+;
我试图打开多个文件来编译其中的数据。我的程序可以编译,但运行时出现以下错误: 在抛出“std::bad_alloc”实例后调用terminate 什么():std::坏的 流产 到目前为止,我的程序相当长,所以我将只链接其中处理打开文件的部分C++ 尝试在c+;中打开多个文件时出错+;,c++,file,vector,C++,File,Vector,我试图打开多个文件来编译其中的数据。我的程序可以编译,但运行时出现以下错误: 在抛出“std::bad_alloc”实例后调用terminate 什么():std::坏的 流产 到目前为止,我的程序相当长,所以我将只链接其中处理打开文件的部分 int main(int argc,char *argv[]) { vector<Plays> yearsEntered; Plays *MyPlays = new Plays(); if (argc < 2) { cout
int main(int argc,char *argv[])
{
vector<Plays> yearsEntered;
Plays *MyPlays = new Plays();
if (argc < 2)
{
cout << "No filenames given." << endl;
return 0;
}
for(int i=1;i < argc; ++i)
{
string filename = argv[i];
cout << filename << endl;
filename.append(".csv");
cout << filename << endl;
ifstream inputFile(filename.c_str(), ios::in);
inputFile.open(filename.c_str());
//Error checking in case file fails to open
if (!inputFile)
{
cout << "Could not open file. " <<
"Try entering another file." << endl;
}
}
intmain(intargc,char*argv[])
{
病媒年数;
播放*我的播放=新播放();
如果(argc<2)
{
无法您已经打开了一次文件。无需再次打开它们
std::ifstream
构造函数正在打开每个文件,然后您无缘无故地调用.open()
。请删除inputFile.open()行
更改此项:
ifstream inputFile(filename.c_str(), ios::in);
inputFile.open(filename.c_str());
为此:
ifstream inputFile(filename.c_str());
你能给我们看一个简短完整的例子吗?这段代码看起来还可以,因此有一些完整的东西可能有助于在真实代码中跟踪问题。你已经打开了你的文件一次。你不需要再次打开它们。std::ifstream
构造函数正在打开它们,然后调用.open()
没有任何原因。删除inputFile.open()
行。@WhozCraig啊,非常感谢。这就是问题所在。干杯!还有,有没有办法将这个问题显示为已回答?@user2777912是的,我会给出一个答案。只需检查左边旁边的绿色标记。