【发布时间】:2016-05-01 05:13:27
【问题描述】:
所以,我正在尝试将文本文件读入 C++ 中的二维数组。 问题是每行的单词数并不总是一样的,一行最多可以包含11个单词。
例如,输入文件可能包含:
ZeroZero ZeroOne ZeroTwo ZeroThree
OneZero OneOne
TwoZero TwoOne TwoTwo
ThreeZero
FourZero FourOne
因此,array[2][1] 应该包含“TwoOne”,array[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()的行,拆分应该是一种方式。 -
你为什么不使用
vector<vector<string>>? -
@barakmanos 我猜应该是
vector<vector<string> > -
@MikeCAT:是的,刚刚解决了这个问题。
-
@MikeCAT “应该是一种方式”是什么意思?
标签: c++ arrays multidimensional-array readfile