Pergunta

Let's say I have a simple text file of 2D points like this:

10
0.000   0.010
0.000   0.260
0.000   0.510
0.000   0.760
0.000   1.010
0.000   1.260
0.000   1.510
0.000   1.760
0.000   2.010
0.000   2.260
// Blank line here

I use a simple struct for IO:

template <typename T>
struct point
{
    point (T x = 0, T y = 0) : x (x), y (y) {}

    T x ;
    T y ;

    friend std::istream& operator>> (std::istream &is, point &p) {
        is >> p.x >> p.y ;
        return is ;
    }
};

My original code was:

int main (void)
{
    std::string strFile = "Points.txt" ;

    std::ifstream file ;
    file.exceptions (std::ios::failbit | std::ios::badbit) ;
    std::vector <point <double> > vec ;

    try {
        file.open (strFile) ;

        int nPoints = 0 ;
        file >> nPoints ;

        for (int n = 0; n < nPoints; ++n) {
            point <double> p ;
            file >> p ;
            vec.push_back (p) ;
        }
    }

    catch (std::ios_base::failure &e) {
        std::cerr << e.what () << "\n" ;
        return 1 ;
    }

    return 0 ;
}

This works fine, but in the spirit of no raw loops, I'd like to get rid of the for-loop.

Here is my new code:

int main (void)
{
    std::string strFile = "Points.txt" ;

    std::ifstream file ;
    file.exceptions (std::ios::failbit | std::ios::badbit) ;
    std::vector <point <double> > vec ;

    try {
        file.open (strFile) ;

        int nPoints = 0 ;
        file >> nPoints ;

        std::copy (
            std::istream_iterator <point <double> > (file), 
            std::istream_iterator <point <double> > (), 
            std::back_inserter (vec)
        ) ;
    }

    catch (std::ios_base::failure &e) {
        std::cerr << e.what () << "\n" ;
        return 1 ;
    }

    return 0 ;
}

Everything is copied fine, but unfortunately, reading the final blank line causes the fail-bit to be set.

The only ways I can think of fixing this are kind of ugly. I could:

  • Put a try-catch clause in the point::operator>>() function
  • Check vec.size() in the catch clause that already exists.

Is there an elegant way of ignoring the final blank line?

Foi útil?

Solução

Change

    std::copy (
        std::istream_iterator <point <double> > (file), 
        std::istream_iterator <point <double> > (), 
        std::back_inserter (vec)
    ) ;

to

    std::copy_n (
        std::istream_iterator <point <double> > (file), 
        nPoints, 
        std::back_inserter (vec)
    ) ;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top