Thread: Parsing a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Well that's what I would do, were it not that the string could be anything, so I'll first have to check if it's even in the form of "%v got too close to %k's grenade", and only if so, get the value of %k and %v

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    sscanf("%v got too close to %k's grenade.","%s got too close to %s's grenade.",v,k);
    I don't know anything about C input, but I looked it up and there are no such symbols as %v and %k. In addition, you don't have the proper number of arguments after the string to match the number of conversion specifiers(e.g. %d, %f).

    So, I think you have more serious problems to deal with.

    I've decided I'm not sure what you are trying to do, but if you have a random string and you are trying to pick either one or two words, I wonder how you expect to do that--what would be the signal to choose two words instead of one?
    Last edited by 7stud; 03-02-2005 at 02:23 PM.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    I think you misunderstood me.
    I'm not using the symbols %k and %v, it's just to show that that the string is in the format:
    "*something* got too close to *somethnigelse*'s grenade."
    And I'm using this to parse it:
    "%s got too close to %s's grenade." (2 times %s, so 2 additional arguments).

    However I'm not certain that the string is in this format, that's what I am trying to check

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I don't use C often so if the following doesn't help and you've not posted to the C Board, that's what I would suggest.

    sscanf() reads from a buffer, rather than the standard input device. Therefore I'd expect the syntax to be something like this:

    char phrase[80] = "It was the best of times";
    char word1[10];
    char word2[10];
    sscanf(phrase, "%s %s", word1, word2);

    where sscanf() will put It in word1 and was in word2.

    Here's a link that might clarify for you, too.

    http://www.cplusplus.com/ref/cstdio/sscanf.html
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Thanks for the reply.
    I'm quite aware of what the sscanf function does and how it does it.
    The problem is actually that the sscanf function does not have the right functionality, for example:

    Code:
    char phrase[80] = "It was the best of times";
    char middle[10];
    sscanf(phrase, "It %s times",middle);
    If I were to write such a function, in the end the value of middle would be "was", rather than "was the best of", which is far more logical.

    I guess what I'm looking for is a string parser that's slightly more intelligent than sscanf

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Once read the value of k will be "%k's" rather than just "%k".
    sscanf("%v got too close to %k's grenade.","%s got too close to %[^'] grenade.",v,k);

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Yea thanks, that's also what I came up with.
    It's not flawless since it still doesn't allow whitespaces, but I guess I'll have to take that for granted then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 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. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM