Reputation: 11
I have a big file with numbers and I want to extract numbers based on their positions(row, column). For example, if I want to process only the first 3 rows and 3 columns which are 9 numbers. The program print the 9 numbers in the first row. I want to go the next line once the column index is 4. How to do this in c++. Here is what I have been don so far:
#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;
}
Upvotes: 0
Views: 378
Reputation: 38893
You should skip lines after each NCOLS
column numbers are read.
#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;
}
Upvotes: 1