因此,我试图将文本文件读入C++中的二维数组中。 问题是,每行中的单词数量并不总是相同的,一行最多可包含11个单词。在C++中将每行中不同数量的文本文本文件读入到二维数组中
例如,输入文件可包含:
ZeroZero ZeroOne ZeroTwo ZeroThree
OneZero OneOne
TwoZero TwoOne TwoTwo
ThreeZero
FourZero FourOne
因此,阵列[2] [1]应包含 “TwoOne”,阵列[1] [1]应包含 “OneOne” 等。
我不知道如何让我的程序每行增加行号。我有什么明显是不工作:
string myArray[50][11]; //The max, # of lines is 50
ifstream file(FileName);
if (file.fail())
{
cout << "The file could not be opened\n";
exit(1);
}
else if (file.is_open())
{
for (int i = 0; i < 50; ++i)
{
for (int j = 0; j < 11; ++j)
{
file >> myArray[i][j];
}
}
}
通过'getline()'读取线条和分割应该是一种方式。 – MikeCAT
你为什么不使用'vector>'? –
@barakmanos我想它应该是'vector> –
MikeCAT