importing data from text file to vector

Good evening everyone.

The following code is meant to import data from a text file and store it in a vector, where the data is a x,y-Point. At the moment, it is not supposed to do anything else with the data. The code is based on a code snippet I found in this forum. The program compiles, but it doesn`t execute. What did I do wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
struct point
{
    double x;
    double y;
   
};

vector<point> points;
ifstream fin;
fin.open("D:\Sebisoft Erweiterung\data.txt");

{
    string first_line; 
    getline(fin, first_line); // waste the first line ("X Y")
}

while( fin.good() )
{
    point temp;
    fin >> temp.x >> temp.y;
    points.push_back(temp);
}
return 0;
}


Any help you might provide is much appreciated.
What do you mean "it doesn't execute"? What makes you think it's not executing?

EDIT: the code looks fine to me (though it would help if you indented properly). You'll have to elaborate on the problem.
Last edited on
Line 18: The \ characters must be escaped. Not doing so results in an invalid file name and therefore your open is failing.

Line 18: You have no check that the open succeeds.

1
2
3
4
5
  fin.open ("D:\\Sebisoft Erweiterung\\data.txt");
  if (! fin.good())
  {  cout << "unable to open input file" << endl;
      exit (1);
  }


Edit: Line 28: You don't check the the fin operation succeeded before you do the push_back. When you hit eof, you going to get an extra entry pushed on the vector.
Last edited on
Thank you, the problem really was the failing opening and me not checking on that.

While we are at it, another problem with the same programm arose with which you guys may also be able to help. I now wanted the program to print the points stored in points to the command window by means of some code I previously used somewhere else to output the contents of a vector. This time, the program doesn`t compile, stating that point is not a valid type for ostream. Do you have any suggestions as to solve or circumvent this problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
struct point
{
    double x;
    double y;
   
};

vector<point> points;
ifstream fin;
fin.open("D:/Sebisoft Erweiterung/data.txt");
 if (!fin.good())
 {
	 cout << "unable to open input file" << endl;
	 system("pause");
		return -1;
 }

{
    string first_line; 
    getline(fin, first_line); // waste the first line ("X Y")
}

while( fin.good() )
{
    point temp;
    fin >> temp.x >> temp.y;
    points.push_back(temp);
}

for(vector<point>::const_iterator i = points.begin(); i != points.end(); ++i)
    cout << *i << ' ';
system("pause");

return 0;
}
i is a point iterator, and dereferencing it returns a point. The problem is exactly what the complier said, it doesn't know how to print a point.

You can print the members of i:
cout << i->x << ' ' << i->y << '\n';

Or tell cout ( an ostream ) how to print a point:
1
2
3
4
5
6
7
ostream& operator<<( ostream& out, const point& point )
{
  return out << '{' << point.x << ", " << point.y << '}';
}

// which will now allow
cout << *i << '\n';
Topic archived. No new replies allowed.