因此,我試圖將文本文件讀入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