Easiest way to scan string to parse out it's parts?

This is a discussion on Easiest way to scan string to parse out it's parts? within the C++ Programming forums, part of the General Programming Boards category; I input a line from a file, into a string called line. I then want to scan it to get ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    21

    Easiest way to scan string to parse out it's parts?

    I input a line from a file, into a string called line. I then want to scan it to get all the individual parts of it, but I'm unsure of how to do that in c++.

    Here's my code:

    Code:
    getline (myfile, line);
    // sscanf(line, "%s %s %s %s", &nickName, &firstName, &secondName, &thirdName);
    // the sscanf line is what I'm trying to do
    It's in a specific format the file I'm scanning in, so it's always 3 or 4 words, separated by spaces (the middle name is sometimes included, sometimes not)
    Last edited by SneakySnake; 02-08-2013 at 10:17 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    You may want to consider using a stringstream to process your string. You could then use the extraction operator>> to extract the different strings to their respective variables.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    Quote Originally Posted by jimblumberg View Post
    You may want to consider using a stringstream to process your string. You could then use the extraction operator>> to extract the different strings to their respective variables.
    you could do that direct from the file, if you know that each line will only ever contain those fields

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    The problem with doing this directly from the file is that the possible missing name will cause problems. With the stringstream it will be easier, in my opinion, to detect this problem because the last entry will be blank, not the name from the next line in the file.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    21
    Jim can you show me an example of what such a line would look like? Scanning for 4 words in the string?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    Did you read the link I provided in my last post. That link has links to several other pages that show examples. For instance this sub-link.

    Jim

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    std::getline does have a delimiter, so:
    Code:
    std::vector<std::string> tokens;
    std::string line;
    while (std::getline(myfile, line, ' '))
       tokens.push_back(line);
    This is also more efficient than stringstream

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to scan string for a number?
    By SneakySnake in forum C Programming
    Replies: 2
    Last Post: 12-07-2012, 09:22 AM
  2. Best way to scan in a string
    By Matt Hintzke in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 06:33 PM
  3. Replies: 1
    Last Post: 04-29-2009, 01:46 PM
  4. scan a string
    By Max in forum C Programming
    Replies: 4
    Last Post: 12-02-2002, 04:26 PM
  5. String Parts
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2002, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21