Reading from .txt file into two dimensional array in c++(从 .txt 文件读取到 C++ 中的二维数组)
问题描述
所以要么我是个彻头彻尾的白痴,这正直盯着我看,但我似乎无法在谷歌或这里找到任何我能理解的资源.
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++ 中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 .txt 文件读取到 C++ 中的二维数组
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
