0

I have a file which contains pixel coordinates in the following format :

234 324
126 345
264 345

I don't know how many pairs of coordinates I have in my file.

How can I read them into a vector<Point> file? I am a beginner at using reading functions in C++.

I have tried this but it doesn't seem to work :

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");

string rBuffer, pBuffer;
Point rPoint, pPoint;

while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    sscanf(rBuffer.c_str(), "%d %d", rPoint.x, rPoint.y);
    sscanf(pBuffer.c_str(), "%d %d", pPoint.x, pPoint.y);

    iP.push_back(pPoint);
    iiP.push_back(rPoint);
}

I receive some odd memory errors. Am I doing something wrong? How can I fix my code so that it can run?

Simon
  • 4,999
  • 21
  • 69
  • 97
  • Don't use printf/scanf in c++, they're not [type-safe](http://stackoverflow.com/questions/260626/what-is-type-safe), use i/o streams (%99 of the time). – enobayram Oct 07 '12 at 06:54
  • I am receiving the error at `sscanf`. How can I read the data from the string so that I can obtain `x` and `y` values accordingly? – Simon Oct 07 '12 at 06:56
  • 2
    Like @enobayram said, don't use `sscanf`. Use the iostreams approach mentioned in my answer. It's much better for your sanity. But if you really, really insist on using `sscanf`, you need to say `&rPoint.x`, and `&rPoint.y` (and ditto with `&pPoint.x` and `&pPoint.y`). – C. K. Young Oct 07 '12 at 07:04

2 Answers2

6

One way to do it is to define a custom input operator (operator>>) for your Point class, then use istream_iterator to read the elements. Here's a sample program to demonstrate the concept:

#include <iostream>
#include <iterator>
#include <vector>

struct Point {
    int x, y;
};

template <typename T>
std::basic_istream<T>& operator>>(std::basic_istream<T>& is, Point& p) {
    return is >> p.x >> p.y;
}

int main() {
    std::vector<Point> points(std::istream_iterator<Point>(std::cin),
            std::istream_iterator<Point>());
    for (std::vector<Point>::const_iterator cur(points.begin()), end(points.end());
            cur != end; ++cur) {
        std::cout << "(" << cur->x << ", " << cur->y << ")\n";
    }
}

This program takes the input, in the format you specified in your question, from cin, then outputs the points on cout in (x, y) format.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
0

Thanks to Chris Jester-Young and enobayram I have managed to solve my problem. I have added bellow my code.

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");
stringstream ss (stringstream::in | stringstream::out);

string rBuffer, pBuffer;


while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    Point bufferRPoint, bufferPPoint;

    ss << pBuffer;
    ss >> bufferPPoint.x >> bufferPPoint.y;

    ss << rBuffer;
    ss >> bufferRPoint.x >> bufferRPoint.y;

    //sscanf(rBuffer.c_str(), "%i %i", bufferRPoint.x, bufferRPoint.y);
    //sscanf(pBuffer.c_str(), "%i %i", bufferPPoint.x, bufferPPoint.y);

    iP.push_back(bufferPPoint);
    iiP.push_back(bufferRPoint);
}
Simon
  • 4,999
  • 21
  • 69
  • 97