Thread: Problems with Extracting String

  1. #1
    Niy
    Guest

    Question Problems with Extracting String

    If I have a string str:

    char str[100] = "\"Chan Tai Man\",\"E\",5000";

    That is, when cout << str;

    the following will appear: "Chan Tai Man","E",5000"

    Now I want to extract the different FIELD of the string into different "substrings", str1, str2, int1
    I wish str1 would hold the three words withOUT the quotes "Chan Tai Man".
    And that str2 hold the single letter E.
    And int1 would hold the integer value of 5000.

    How can I do so?

    I have used normal <string.h> technique with loops but in vain.
    And I have used scanf technique:

    char str1[50], str2[50], str3[50];
    char str[100] = "\"Chan Tai Man\",\"E\",5000";
    // read fields using scanset
    sscanf(str, "%s%s%i", str1, str2, str3);
    cout << "str1 = " << str1 << ", str2 = " << str2 << ", str3 = " << str3 << endl;

    But scanf always stop reading when it meets the space characters. It make str = "Chan which I dont want. And the str is of string type but I wanted integer type.

    So many problems, Anyone can help???

    Thanks~

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    I would recommend that you use the C++ string class. It has several neat member funcitons that will solve your problems quite easily.

  3. #3
    Niy
    Guest

    Question

    Originally posted by Hannwaas
    I would recommend that you use the C++ string class. It has several neat member funcitons that will solve your problems quite easily.
    Can you explain more explicitly? Thanks~

    P.S. I am still working on my sscanf. It still stop reading when meeting a space character. That makes me not able to read the whole name "Tony IX McGeorage" into a single string.

  4. #4
    Unregistered
    Guest
    You could try reading the string one char at a time.
    If the current char is " ignore it. If the current char is , then place a null char at the end of the current substring and start a new substring. Otherwise place current char at end of current substring.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM