-1

I am very new to C++ and am trying to create a vector of points in a 2d plane read from a textfile. To do this I first create a struct consisting of two values (x,y) called point. Then a vector of these points called vec. However I am unsure of how to populate the struct data when the textfile is in three columns! The first column is just an index for the points, the second column is the x data and the third is the y data. I don't know the size of vec so I try to use push_back() Here is what I have so far.

    int main(){

        struct point{
 std::vector<x> vec_x;
 std::vector<y> vec_y;
    };

    std::vector<point> vec;
    vec.reserve(1000);
    push_back();

    ifstream file;
    file.open ("textfile.txt");
        if (textfile.is_open()){


/* want to populate x with second column and y with third column */
        }
        else std::cout << "Unable to open file";
    }

Where the comment is I have the following;

while( file  >> x ) 
vec.push_back (x);


while( file  >> y ) 
vec.push_back (y);

Apologies if this is very simple, but it isn't to me! Posted below is an example of the txt file with only 6 points.

0 131 842
1 4033 90
2 886 9013490
3 988534 8695
4 2125 10
5 4084 474
6 863 25

EDIT

while (file >> z >> x >> y){

    struct point{
        int x;
        int y;
    };

    std::vector<point> vec;
    vec.push_back (x);
    vec.push_back (y);

}
AngusTheMan
  • 564
  • 1
  • 6
  • 15

2 Answers2

4

You can use the normal input operator >> in a loop:

int x, y;  // The coordinates
int z;     // The dummy first value

while (textfile >> z >> x >> y)
{
    // Do something with x and y
}

As for the structure, I suggest a little different approach:

struct point
{
    int x;
    int y;
};

Then have a vector of structures:

std::vector<point> points;

In the loop, create a point instance, initialize its x and y member, then push it back into the points vector.

Note that the above code have almost no kind of error checking or fault tolerance. If there is an error in the file, more specifically if there is a problem with the format (for example one extra number in one line, or a number to little) then the code above will not be able to handle it. For that you can use std::getline to read the whole line, put it into a std::istringstream and read into the x and y variables from the string stream.


Putting it all together, the simple example of working code (without handling of invalid input) would then be something like

#include <fstream>
#include <vector>

// Defines the point structure
struct point
{
    int x;
    int y;
};

int main()
{
    // A collection of points structures
    std::vector<point> points;

    // Open the text file for reading
    std::ifstream file("textfile.txt");

    // The x and y coordinates, plus the dummy first number
    int x, y, dummy;

    // Read all data from the file...
    while (file >> dummy >> x >> y)
    {
        // ... And create a point using the x and y coordinates,
        // that we put into the vector
        points.push_back(point{x, y});
    }

    // TODO: Do something with the points
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
  1. Read a line using std::getline()

  2. Split the string for white spaces as mentioned here

  3. push every element to your vectors.

  4. repeat for next line until the end of the file

Community
  • 1
  • 1
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189