問題描述
所以要么我是個徹頭徹尾的白癡,這正直盯著我看,但我似乎無法在谷歌或這里找到任何我能理解的資源.
So either I'm a complete idiot and this is staring me right in the face, but I just can't seem to find any resources I can understand on google, or here.
我有一個包含多行整數的文本文件,每個整數用空格分隔,我想將這些整數讀入一個數組,其中每一行是數組的第一維,每個整數在那條線上被保??存到第二維中.
I've got a text file which contains several lines of integers, each integer is separated by a space, I want to read these integers into an array, where each new line is the first dimension of the array, and every integer on that line is saved into the second dimension.
可能用了最糟糕的術語來解釋,抱歉.
Probably used the worst terminology to explain that, sorry.
我的文本文件如下所示:
My text file looks something like this:
100 200 300 400 500
101 202 303 404 505
111 222 333 444 555
我希望得到的數組是這樣的:
And I want the resulting array to be something like this:
int myArray[3][5] = {{100, 200, 300, 400, 500},
{101, 202, 303, 404, 505},
{111, 222, 333, 444, 555}};
推薦答案
我相信
istream inputStream;
int myArray[3][5];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 5; j++)
istream >> myArray[i][j];
應該做你需要的.
這篇關于從 .txt 文件讀取到 C++ 中的二維數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!