在C ++中从.txt文件读取到二维数组 [英] Reading from .txt file into two dimensional array in c++

查看:91
本文介绍了在C ++中从.txt文件读取到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要么是一个完全的白痴,这使我直视了我的脸,但我似乎无法在Google或这里找到我能理解的任何资源.

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];

应该做你所需要的.

这篇关于在C ++中从.txt文件读取到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆