Thread: Parse the string

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Parse the string

    I need to parse the following string:

    4210650,"GENERAL MCLANE SCHOOL DISTRICT","KG-12",18037,2862,142

    int each indavidual part seperated by the commas and with the quotation marks removed?

    I use getline to bring it in as a temp string.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Put the temp string into a stringstream, then use getline again, but this time with ',' as the delimiter. Then convert the values that don't start with a quote to int (or whatever), and the ones that do you can just use erase to erase the first and last characters from the string.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    So what you are saying is to have a string say

    temp_string.

    and use getline for on it.......what would be the correct syntax to do that....I thought getline was only for fstreams????

  4. #4
    Since a string is really an array of characters, you could read in each char from the string untill a , is encountered.

    If you encounter a quotation, do not read it in to the new string, but read into a string.

    Otherwise, read into a temporary string, and then convert it into an integer.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The getline function is for any input stream: cin, ifstream, istringstream and others. Once you have your temp_string, create an istringstream and pass it as a parameter to the constructor. Then you can use your istringstream with getline. If you haven't already, I'm sure you could search and find some examples of istringstream or just stringstream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM