根据数字在 c++ 中作为矩阵的位置从文件中读取数字
Read numbers from file based on their position as matrix in c++
我有一个包含数字的大文件,我想根据它们的位置(行,列(提取数字。例如,如果我只想处理前 3 行和 3 列,即 9 个数字。程序打印第一行中的 9 个数字。一旦列索引为 4,我想转到下一行。如何在 c++ 中执行此操作。以下是我到目前为止所做的事情:
#include <iostream>
#include <fstream>
using namespace std;
const int NROWS = 3;
const int NCOLS = 3;
int main()
{
ifstream data;
double Numbers[NROWS][NCOLS];
double Num;
data.open("Input.txt");
for (int i = 0; i<NROWS; ++i)
{
for (int j = 0; j<NCOLS; ++j)
{
data >> Num;
Numbers[i][j]=Num;
cout << Numbers[i][j] << endl;
}
}
data.close();
return 0;
}
您应该在读取每个NCOLS
列号后跳过行。
#include <iostream>
#include <fstream>
using namespace std;
const int NROWS = 3;
const int NCOLS = 3;
int main()
{
ifstream data;
double Numbers[NROWS][NCOLS];
double Num;
data.open("Input.txt");
for (int i = 0; i<NROWS; ++i)
{
for (int j = 0; j<NCOLS; ++j)
{
data >> Num;
Numbers[i][j]=Num;
cout << Numbers[i][j] << endl;
}
std::string skip;
std::getline(data, skip);
}
data.close();
return 0;
}
相关文章:
- 如何找到数字的位置并使用它们
- 统一位置和属性位置 - 是相同的数字系列吗?
- 数字的总和在奇数,甚至是字符串的位置
- 根据数字在 c++ 中作为矩阵的位置从文件中读取数字
- 计算正确和放错位置的数字
- 如何将数字0-8变成C 的0-8二进制位置
- 你如何在序列中找到数字的位置
- C 更改数字位置
- 我可以读取向量<string>::迭代器的数字位置吗?
- 将一个数字的一个数字与其当前位置相乘,然后使用递归将其和其他数字相加
- C++如何从文件中读取数字并将其分配到数组中的相应位置
- 构建一个程序,将 10 个整数放入数组中并识别重复的数字并输出它们及其位置编号的位置
- 按每行同一位置出现的 4 个数字对文本文件中的 200 行进行排序
- 我需要从用户输入中找到某些数字的位置
- 带有Overload[]运算符的C++MyInteger类,因此索引在位置i返回数字
- (c++)对数组中的重复项进行计数,然后将被计数的数字移动到另一个数组中的相应位置
- 我需要知道如何在c++中将数字四舍五入到小数点后的某个位置
- 找出数组中最小的数字及其位置
- 交换两个数字而不使用额外的空间,如果两个数字都指向相同的位置,则不起作用
- 用户正在输入10个数字,找到最大的负值及其索引/位置