Retrieving a particular line from list<string>

Discussion in 'C' started by batcave109, Apr 7, 2009.

  1. batcave109

    batcave109 New Member

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I have read a .txt file into a list<string> container and now I am trying to pull out individual lines that meet certain criteria. For example the text file I am reading from is layed out like a table:
    Name age sex
    Peter 25 m
    Paul 32 f
    Jenny 19 f

    I want to retrieve a line by searching for the name e.g passing a "Peter" argument will return "Peter 25 m" and afterwards I want to split this line up into individual elements "Peter", 25 and 'm'
    How would I go about this?
    Here is the code I have so far.

    Code:
    int main()
    {    
        string temp;
        ifstream fileInput;
        list<string> L;
    
        fileInput.open("Data.txt");
        if(!fileInput)
            {
                cerr<< "file cannot be found";
            }
        else
            {
                while( getline(fileInput, temp)){
                     L.push_back( temp ); 
                 }
            }
    and then to try and pull individual lines out

    Code:
    std::list<std::string>::iterator i;
    
        for (i = input.begin(); i != input.end(); ++i)
    {
       if (*i == "Peter 25 m")
       {
          break;
       }
    }
    
    if (i != input.end())
    {
        std::cout<< *i;
    }
    else
    {
        std::cout  "not found line";
    }
    The above snippet works however it only outputs the line if the full line is matched exactly instead of just matching the name.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The C function strstr() tells you if a string exists within another string, e.g. strstr("Peter","Peter 25 m") would return TRUE. I'm not sure if std::string has anything like that; if not then you would need to call c_str() for the parameters to strstr().
     
  3. batcave109

    batcave109 New Member

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    That works, however the arguments are the reverse of what you put e.g strstr("Peter 25 m", "Peter") instead of strstr("Peter", "Peter 25 m") and I did have to call c_str() like you said.
    Thanks
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sorry about that; I should have RTFM...I never can remember parameter lists.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice