0

I am new to C++ programming and I am trying to open a text file where most of the information isn't useful to me. The file looks like that:

1

547

troncons

0 1 2 ...

2 2 3 4 5 7 ...

0 4 5 ...

...

So as you can see each line has a different length. What I want to extract first is the second element (547) which tell me the number of line after "troncons". After that, the number in the first column tell me how the information is distributed in the line so if it's 0, that means the second value is x and the third is y, but if the first value isn't 0 then the second value is x, the third is the number n of y values and the n next value are the y associated to the x. I'm only working with integer and I am trying to create an array of dimension (i,2) where each line is a (x,y) couple. Also there are other values at the end of each, but I don't need it.

I got an idea on how my code should work, but I don't know how to write it in C++ since I'm used to Matlab

  1. get the number of line either by extracting the value in the second line or by getting the total number of line and subtracting 3.

  2. Iterate on each line and use a conditional statement to know if the first element is 0 or not.

  3. Then if it's 0, add the second and the third value to the array.

  4. If it is !=0, get the third value and iterate over that number to add in the array the x from the second value and the y from the 3+i value.

That's how I did it in Matlab because they automatically put the text in a matrix and it is easy to access it with index, but I feel you can't do that with C++.

I saw those two links: How do i read an entire .txt file of varying length into an array using c++?

Reading matrix from a text file to 2D integer array C++

But the first one used a vector or a one dimensonal array and the second one took all the information directly and used static memory.

Community
  • 1
  • 1
Dom C.
  • 35
  • 9
  • 1
    _"I don't know how to write it in C++ since I'm used to Matlab"_ Time to learn C++, or ask a friend who knows C++ to write it for you. Or hire a consultant to do so. – Lightness Races in Orbit May 29 '15 at 16:41
  • Reading in a file line by line is described in the answer of [this question][1]. [1]: http://stackoverflow.com/questions/7868936/read-file-line-by-line – Dko May 29 '15 at 17:08

1 Answers1

0

This should get you going:

#include <fstream>
#include <vector>
#include <iostream>
#include <string>

struct XY {
   int x;
   int y;
};

using std::ifstream;
using std::vector;
using std::cerr;
using std::endl;
using std::string;

int main() {
   vector<XY> data;
   ifstream input("filename.txt");

   if (input.good()) {
      int skip;
      string troncons;
      int numLines;
      input >> skip;
      input >> numLines; // 547
      input >> tronscons;
      data.reserve(numLines); // optional, but good practice
      for (int i=0; i<numLines; ++i) {
         XY xy;
         int first;
         input >> first;
         if (first == 0) {
            input >> xy.x;
            input >> xy.y;
            data.push_back(xy);
         } else {               
            int n;
            input >> xy.x;
            input >> n;
            for (int j=0; j<n; ++j) {
               input >> xy.y;
               data.push_back(xy);
            }
         }
      }
   }

   return 0;
}
Mustafa Ozturk
  • 794
  • 3
  • 18