正在用c++从文件中读取整数
Reading integers from file in c++
我目前有一个纯文本文件,其中包含三个表,如下所示:
0 0 0 0
20 20 0 0
100 150 150 150
100 0 0 0
0 255 255 255
0 0 0 255
20 100 100 100
0 0 0 0
100 100 250 250
255 255 0 0
0 100 255 0
20 100 100 100
0 0 0 0
100 20 20 100
0 255 255 255
每个表表示图像的RGB值。第一张桌子全红,第二张桌子全绿,第三张桌子全蓝。我有int数组red[][]、green[][]和blue[][][],我想将这些值存储到这些数组中。
我现在有一个循环:
string data;
int count = 0;
while (getline(infile, data))
{
// iterate though data line and store into array
count++;
}
我肯定知道,如果计数<5我应该存储到红色数组中,<11放入绿色数组等,但我不确定如何将每个单独的数字取出存储。最好的方法是什么?
使用data
字符串初始化istringstream
并提取int
,例如:
while (getline(infile, data))
{
std::istringstream iss(data);
int i, j, k;
iss >> i >> j >> k;
count++;
}
相关文章:
- 如何在c++中从文本文件中逐行读取整数
- Arduino从Txt读取整数
- 从二进制文件中读取整数数组
- 使用字符串流读取整数并将它们相加
- 如何在从文件中读取整数时使用 file.eof()?
- C++从键盘读取整数的功能
- 为什么字符串流只读取整数中的第一个数字?如何使它只在空白处移动?
- 如何从文本文件C++读取整数和特殊字符
- C++ 从控制台读取整数数组
- 从文本文件中读取整数并将它们存储到数组中
- 输入重定向读取整数和char C
- 从文件读取整数到矩阵 (c++)
- 如何读取整数直到线结束
- 从输入文件读取整数,仅存储数组C 中的唯一整数
- 从文本文件中读取整数
- 读取整数对,直到文本输入文件中的换行符
- 在C++中从文件中读取整数和字符
- 使用 MFC C++ 6.0 从 ms 访问中读取整数数据
- 从标准输入和输入值中读取整数到向量
- 从文件中读取整数到数组