skipping line while reading data file

Discussion in 'C++' started by Ivan Liu, Sep 4, 2006.

  1. Ivan Liu

    Ivan Liu Guest

    I would like know how I can skip a line while reading
    a set of input data (from a text file) if the first character
    of the line is "#".

    My original code reads:

    ifstream Infile("data.dat");

    for( int i=0; i<N; i++){
    Infile >> x >> y;
    }
    Ivan Liu, Sep 4, 2006
    #1
    1. Advertising

  2. Ivan Liu

    Jerry Coffin Guest

    In article <>,
    says...
    > I would like know how I can skip a line while reading
    > a set of input data (from a text file) if the first character
    > of the line is "#".
    >
    > My original code reads:
    >
    > ifstream Infile("data.dat");
    >
    > for( int i=0; i<N; i++){
    > Infile >> x >> y;
    > }


    One obvious possibility would be something like this:

    std::istream &my_getline(std::istream &is, std::string &s) {
    while (std::getline(is, s) && s[0] == '#')
    ;
    return is;
    }

    for (int i=0; i<N; i++) {
    std::string line;
    if (!my_getline(Infile, line))
    break;
    std::stringstream temp(line);
    temp >> x >> y;
    }

    That does raise one minor question: you're starting with a count (N) of
    lines to read. Is that count supposed to include the commented lines or
    not? The code above attempts to read N lines of actual data -- if you
    want a total of N lines, whether they contain data or not, you'd have to
    rewrite it a bit.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 4, 2006
    #2
    1. Advertising

  3. Ivan Liu

    Ivan Liu Guest

    Thanks. I have a further question..


    Jerry Coffin wrote:

    > One obvious possibility would be something like this:
    >
    > std::istream &my_getline(std::istream &is, std::string &s) {
    > while (std::getline(is, s) && s[0] == '#')
    > ;
    > return is;
    > }
    >
    > for (int i=0; i<N; i++) {
    > std::string line;
    > if (!my_getline(Infile, line))
    > break;


    Is the if-statement for checking if the Infile does exit?



    > std::stringstream temp(line);
    > temp >> x >> y;
    > }
    >
    Ivan Liu, Sep 5, 2006
    #3
  4. Ivan Liu

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > > for (int i=0; i<N; i++) {
    > > std::string line;
    > > if (!my_getline(Infile, line))
    > > break;

    >
    > Is the if-statement for checking if the Infile does exit?


    It's mostly to check whether we've reached the end of the file (for
    example, if the file got corrupted and only contained one line).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Vidal
    Replies:
    3
    Views:
    2,212
  2. Mik0b0
    Replies:
    12
    Views:
    510
    David Thompson
    Apr 15, 2007
  3. MRAB
    Replies:
    3
    Views:
    327
    Lionel
    Feb 5, 2009
  4. Lionel
    Replies:
    2
    Views:
    324
    Slaunger
    Feb 6, 2009
  5. Replies:
    27
    Views:
    399
    Peter J. Holzer
    May 18, 2007
Loading...

Share This Page