我正在为作业做的一些代码有问题。当我去编译文件时,它有时可以工作,有时不能。该程序的基本思想是从文件中读取每一行文本并将其存储到一个数组中(数组的大小应该是 100,应该有 100 行文本)。每个文本字符串(每一行)都应该存储在它自己的数组地址中。一旦存储了所有行,程序将从数组中拉出每一行,注意它来自哪个行号。当用 Code::Blocks 编译它时,它运行没有问题,但是,当我用 cygwin 编译它时,我去运行它并得到一条错误消息,上面写着“在抛出一个 'std::bad_cast' 实例后调用终止什么( ): std::bad_cast 中止(核心转储)"
你们能给我的任何帮助将不胜感激!
这是我到目前为止得到的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string aFile[100];
ifstream nFile("TMA1Question4 Text.txt");
string nText;
if (nFile)
{
for (int nLineCounter=1; nLineCounter <=100; getline(nFile, nText))
{
aFile [nLineCounter] = nText;
nLineCounter++;
}
}
for (int nLineReader=1; nLineReader<=100; nLineReader++)
{
cout << "Line" << nLineReader << ": " << aFile[nLineReader] << endl;
}
return 0;
}