0

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;
  }
Mike Aguilar
  • 348
  • 2
  • 3
  • 14
Adam
  • 11
  • 4
  • _"Here is what I have been don so far"_ And? Does it work? Presumably not, so what is the problem? It's unclear what you're asking. – underscore_d Apr 18 '18 at 14:12
  • It is reading the first 9 numbers in the file. For example the input file is : First line : 1 2 3 4 5 6 7 8 9 Second Line: 10 11 12 13 14 15 16 17 18 and I want to read only: 1 2 3 10 11 12 . The program I wrote read 1 2 3 4 5 6 7 8 9. – Adam Apr 18 '18 at 14:24
  • I don't want to read the whole line of numbers in the first row. – Adam Apr 18 '18 at 14:25
  • That's good info! Please [edit] your question to contain that, rather than putting it in comments. – underscore_d Apr 18 '18 at 14:26
  • 1
    Take a look at option 2 of this answer: https://stackoverflow.com/a/7868998/4581301 . Basically, try to read a line. Place line into `stringstream`. Read `NCOLS` values from `stringstream`. Repeat until you either can't read a line (file too short) or you've read `NROWS` lines. – user4581301 Apr 18 '18 at 17:06

1 Answers1

1

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;
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • This command std::getline(data, skip); doesn't work. The output now are all 0's. – Adam Apr 18 '18 at 14:29
  • This skip like 10 rows not going to the next line. – Adam Apr 18 '18 at 14:53
  • It can not skip 10 lines, you loop is limited by `NROWS`. You did smth wrong or your input data is not correct. – 273K Apr 18 '18 at 16:05
  • 1
    Rather than a `getline` (copies into `string`), consider `ignore(numeric_limits::max(), '\n');` (discards all data to end of line. No copy. No `string` resize overhead). – user4581301 Apr 18 '18 at 17:13