skipping line while reading data file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Liu

    skipping line while reading data file

    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.da t");

    for( int i=0; i<N; i++){
    Infile >x[i] >y[i];
    }

  • Jerry Coffin

    #2
    Re: skipping line while reading data file

    In article <1157382384.891 972.312980@74g2 000cwt.googlegr oups.com>,
    darknails@gmail .com 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.da t");
    >
    for( int i=0; i<N; i++){
    Infile >x[i] >y[i];
    }
    One obvious possibility would be something like this:

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

    for (int i=0; i<N; i++) {
    std::string line;
    if (!my_getline(In file, line))
    break;
    std::stringstre am temp(line);
    temp >x[i] >y[i];
    }

    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.

    Comment

    • Ivan Liu

      #3
      Re: skipping line while reading data file

      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(i s, s) && s[0] == '#')
      ;
      return is;
      }
      >
      for (int i=0; i<N; i++) {
      std::string line;
      if (!my_getline(In file, line))
      break;
      Is the if-statement for checking if the Infile does exit?


      std::stringstre am temp(line);
      temp >x[i] >y[i];
      }
      >

      Comment

      • Jerry Coffin

        #4
        Re: skipping line while reading data file

        In article <1157458866.942 853.265650@h48g 2000cwc.googleg roups.com>,
        darknails@gmail .com says...

        [ ... ]
        for (int i=0; i<N; i++) {
        std::string line;
        if (!my_getline(In file, 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.

        Comment

        Working...