I have a following container which I would like to store in a file:
std::vector< std::vector< Point > > m_vPoints;
Point
is a basic structure with defined operator<<()
and operator>>()
.
The format of the file is:
point0_0 point0_1 point0_2 ... <-- points from m_vPoints[0]
point1_0 point1_1 ... <-- points from m_vPoints[1]
...
point elements are separated by ','
, and points are separated by ' '
e.g.:
-5,4 6,12 -7,32 ...
12,0 -3,4 ...
I managed to produce such a file with:
std::ostream& operator<<(std::ostream &o, ...)
{
for(auto it=m_vPoints.begin(); it!=m_vPoints.end(); ++it)
{
copy(it->begin(), it->end(), std::ostream_iterator<Point>(o," "));
o << endl;
}
return o;
}
and it works fine. The problem is however with reading. When I try the following:
std::istream& operator>>(std::istream &is, ...)
{
int numberOfRows; // assume it is known and valid
m_vPoints.resize(numberOfRows);
for(int i=0; i<numberOfRows; i++)
{
copy(std::istream_iterator<Point>(is), std::istream_iterator<Point>(), std::back_inserter(m_vPoints[i]));
}
return is;
}
all points from all lines are read into m_vPoints[0]
. It looks like std::istream_iterator<Point>()
ignores std::endl
. Using is >> noskipws
will not work, because I still want to skip whitespaces that separate individual points. So basically, the problem is to copy from the istream but not until end-of-stream is reached but until end-of-line. Examples I saw in the net using cin
somehow manage to parse a single line, ignoring space characters, but correctly ending at std::endl
or '\n'
I'd also like to avoid getline()
, as then I'd have to copy each line to a string and then parse the string. Instead, I'd like to copy line i
directly to container m_vPoints[i]