#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    304
    Rep Power
    24

    C++ parsing file readline input


    Hi,

    I'm new to C++ and I'm trying to find some simple info on parsing the string from a readline call on an input file. Each line in the input file contains data separated by a space, which needs to summed up. I've only learned how to get each line of the input file using a loop and the readline function, but not how to break the string down into its separate data. Can someone show me how or point me to some docs?

    Thanks
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Simply use the >> operator instead of getline
    Code:
    string foo;
    fin >> foo;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    304
    Rep Power
    24
    Originally Posted by Scorpions4ever
    Simply use the >> operator instead of getline
    Code:
    string foo;
    fin >> foo;
    Each line needs to be summed individually though, and not everything in the line can be summed. This is why readline was more convenient.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Okay...let's say the line is stored as a string s.
    Code:
    istringstream ins(s);
    int num;
    while (s >> num) {
       // ...
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    304
    Rep Power
    24
    Originally Posted by Lux Perpetua
    Okay...let's say the line is stored as a string s.
    Code:
    istringstream ins(s);
    int num;
    while (s >> num) {
       // ...
    }
    Hi,
    I still don't see how that helps. Here is a sample of a line so you can understand what I mean:

    ABC-D-12 5 28 29

    Only the 5, 28, and 29 need to be summed. The first part (ABC-D-12) is just an ID, which needs to be outputted into a file. Knowing this, how can I access each piece of data?

    Thanks
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    C++ Code:
    #include <iostream>
    #include <string>
    #include <sstream>
     
    int main()
    {
       std::string id, s("ABC-D-12 5 28 29");
       std::istringstream iss(s);
       int value, sum = 0;
       if ( iss >> id )
       {
          while ( iss >> value )
          {
             sum += value;
          }
       }
       std::cout << "ID: " << id << ", sum = " << sum << '\n';
       return 0;
    }
     
    /* my output
    ID: ABC-D-12, sum = 62
    */
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    304
    Rep Power
    24
    Thank you.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    14
    Rep Power
    0
    Thank you Dave! I just happened to be having a VERY similar problem today, and after almost 2 hours of looking over various websites and texts, I found this thread, and your answer for black_lotus was just what I needed too. So...thanks! ^_^

IMN logo majestic logo threadwatch logo seochat tools logo