Help, read input from file ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tvn007@hotmail.com

    #1

    Help, read input from file ?

    Please help,
    basicly, I just want to skip "#" when read line from input file.
    Below is my code:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;

    struct Record_info {
    string name;
    double midterm;
    double quiz;
    double final;
    }student;
    int main (void){

    double grade;

    ifstream in ("test2.txt" );
    string line,word;
    while (getline(in,lin e)){
    if (line.find_firs t_of("#")) continue; //Just want to skip "#" when
    getline
    istringstream anyname(line);

    anyname>>studen t.name>>student .midterm>>stude nt.quiz>>studen t.final;
    cout << student.name <<endl;
    cout << student.midterm <<endl;
    cout << student.quiz <<endl;
    cout << student.final <<endl;
    }
    return 0;
    }

    //////////// input file: test2.txt /////////
    ##############
    Tony 90.0 -15.2 98.2
    Michael 95.0 17.2 92.2

    ////////////// output from program ////
    ##############
    0
    0
    0
    ////////////////// I would like to have the output as follow ???????
    Tony
    90.0
    -15.2
    98.2
    Michael
    95.0
    17.2
    92.2

  • Rolf Magnus

    #2
    Re: Help, read input from file ?

    tvn007@hotmail. com wrote:
    [color=blue]
    > Please help,
    > basicly, I just want to skip "#" when read line from input file.
    > Below is my code:
    >
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <sstream>
    > using namespace std;
    >
    > struct Record_info {
    > string name;
    > double midterm;
    > double quiz;
    > double final;
    > }student;
    > int main (void){
    >
    > double grade;
    >
    > ifstream in ("test2.txt" );
    > string line,word;
    > while (getline(in,lin e)){
    > if (line.find_firs t_of("#")) continue; //Just want to skip "#" when
    > getline[/color]

    find_first_of() will return 0 if, and only if the first character of your
    string (i.e. the one with index 0) is a '#'. This is only the case for the
    first line, so it's the only one that will not be ignored.
    And you actaully don't need find_first_of, which looks for one of a list of
    characters. Since your "list" only consists of one character, you can
    simply use find(). I'm not sure what exactly you want. Do you want to
    ignore lines that begin with a '#' or those that contain '#' at any place?
    In the former case, try:

    if (line.size >= 1 && line[0] == '#') continue;

    For the latter, try:

    if (line.find('#') != string::npos) continune;

    Comment

    • Jim Langston

      #3
      Re: Help, read input from file ?

      <tvn007@hotmail .com> wrote in message
      news:1132502364 .301063.49410@g 49g2000cwa.goog legroups.com...[color=blue]
      > Please help,
      > basicly, I just want to skip "#" when read line from input file.
      > Below is my code:
      >
      > #include <iostream>
      > #include <string>
      > #include <fstream>
      > #include <sstream>
      > using namespace std;
      >
      > struct Record_info {
      > string name;
      > double midterm;
      > double quiz;
      > double final;
      > }student;
      > int main (void){
      >
      > double grade;
      >
      > ifstream in ("test2.txt" );
      > string line,word;
      > while (getline(in,lin e)){
      > if (line.find_firs t_of("#")) continue; //Just want to skip "#" when
      > getline
      > istringstream anyname(line);
      >
      > anyname>>studen t.name>>student .midterm>>stude nt.quiz>>studen t.final;
      > cout << student.name <<endl;
      > cout << student.midterm <<endl;
      > cout << student.quiz <<endl;
      > cout << student.final <<endl;
      > }
      > return 0;
      > }
      >
      > //////////// input file: test2.txt /////////
      > ##############
      > Tony 90.0 -15.2 98.2
      > Michael 95.0 17.2 92.2
      >
      > ////////////// output from program ////
      > ##############
      > 0
      > 0
      > 0
      > ////////////////// I would like to have the output as follow ???????
      > Tony
      > 90.0
      > -15.2
      > 98.2
      > Michael
      > 95.0
      > 17.2
      > 92.2[/color]

      The way I did it in my code:

      std::ifstream InFile(FileName .c_str());
      if (InFile.is_open ())
      {
      while (! InFile.eof() )
      {
      std::string Buffer;
      InFile >> Buffer;
      while ( Buffer[0] != '#' && ! InFile.eof())
      {
      std::getline( InFile, Buffer );
      InFile >> Buffer;
      }
      if ( !InFile.eof() )
      {
      if ( Buffer == "#DAY_AMBIE NT" )
      ...
      }


      Comment

      • Jim Langston

        #4
        Re: Help, read input from file ?

        "Jim Langston" <tazmaster@rock etmail.com> wrote in message
        news:zFagf.3806 0$7s1.2655@fe04 .lga...[color=blue]
        > <tvn007@hotmail .com> wrote in message
        > news:1132502364 .301063.49410@g 49g2000cwa.goog legroups.com...[color=green]
        >> Please help,
        >> basicly, I just want to skip "#" when read line from input file.
        >> Below is my code:
        >>
        >> #include <iostream>
        >> #include <string>
        >> #include <fstream>
        >> #include <sstream>
        >> using namespace std;
        >>
        >> struct Record_info {
        >> string name;
        >> double midterm;
        >> double quiz;
        >> double final;
        >> }student;
        >> int main (void){
        >>
        >> double grade;
        >>
        >> ifstream in ("test2.txt" );
        >> string line,word;
        >> while (getline(in,lin e)){
        >> if (line.find_firs t_of("#")) continue; //Just want to skip "#" when
        >> getline
        >> istringstream anyname(line);
        >>
        >> anyname>>studen t.name>>student .midterm>>stude nt.quiz>>studen t.final;
        >> cout << student.name <<endl;
        >> cout << student.midterm <<endl;
        >> cout << student.quiz <<endl;
        >> cout << student.final <<endl;
        >> }
        >> return 0;
        >> }
        >>
        >> //////////// input file: test2.txt /////////
        >> ##############
        >> Tony 90.0 -15.2 98.2
        >> Michael 95.0 17.2 92.2
        >>
        >> ////////////// output from program ////
        >> ##############
        >> 0
        >> 0
        >> 0
        >> ////////////////// I would like to have the output as follow ???????
        >> Tony
        >> 90.0
        >> -15.2
        >> 98.2
        >> Michael
        >> 95.0
        >> 17.2
        >> 92.2[/color]
        >
        > The way I did it in my code:
        >
        > std::ifstream InFile(FileName .c_str());
        > if (InFile.is_open ())
        > {
        > while (! InFile.eof() )
        > {
        > std::string Buffer;
        > InFile >> Buffer;
        > while ( Buffer[0] != '#' && ! InFile.eof())
        > {
        > std::getline( InFile, Buffer );
        > InFile >> Buffer;
        > }
        > if ( !InFile.eof() )
        > {
        > if ( Buffer == "#DAY_AMBIE NT" )
        > ...
        > }
        >
        >[/color]

        Oooops, mine does the opposite. Mine ignores any line NOT starting with #.


        Comment

        Working...