Thread: Parsing a string

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    18

    Parsing a string

    I am trying to parse a game's logfile, and I stumbled across a problem.
    The problem is that I can't seem to be able to parse the following string:
    %v got too close to %k's grenade.
    using:
    Code:
    char v[100];
    char k[100];
    sscanf("%v got too close to %k's grenade.","%s got too close to %s's grenade.",v,k);
    Once read the value of k will be "%k's" rather than just "%k".

    Another problem is that either %k or %v can be more than one word, but I would be happy enough if I fix the first problem.

    I hope somebody knows a solution to my problem

    Thanks in advance,
    Borre Mosch

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Once read the value of k will be "%k's" rather than just "%k".
    You could do something like this:

    string str;
    inFile>>str;
    str = str.substr(0, str.length() - 2);

    Another problem is that either %k or %v can be more than one word, but I would be happy enough if I fix the first problem.
    You could check to see if there is a space in the string. If there is, get the substring up to the space and the substring after the space. Then, change the second substring like above, and join the substrings back together.
    Last edited by 7stud; 03-02-2005 at 11:06 AM.

  3. #3
    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

  4. #4
    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.

  5. #5
    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

  6. #6
    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.

  7. #7
    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

  8. #8
    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);

  9. #9
    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.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    Lightbulb

    if you're worried about whitespace, convert all whitespaces to a character you're sure won't be used anywhere else, say underscore (you can change it to whatever) and then change it back afterwards. here are some example functions to get you started:

    Code:
    #include <iostream>
    using namespace std;
    void removeWhitespace(char[] input, int len) {
        for (int i = 0; i < len; i++) {
            if (input[i] == " ") {
                input[i] = "_"; //this is line you change to alter the character--change the underscore to whatever you want
            }
        }
    }
    
    void putWhitespaceBack(char[] input, int len) {
        for (int i = 0; i < len; i++) {
            if (input[i] == "_") { //if you change the underscore, you'll have to change it here also
                input[i] = " ";
            }
        }
    }
    
    int main() {
        char mystr[100] = "Hello, my name is Bob";
        cout << mystr << endl;
        removeWhitespace(mystr, 100);
        cout << mystr << endl;
        putWhitespaceBack(mystr, 100);
        cout << mystr << endl;
        return 0;
    }
    you don't have to return input[] from those functions because when you pass an array it is like a pointer so THE TWO FUNCTIONS ARE OPERATING DIRECTLY ON THE ARRAY. that can be good and bad.

    this is in c++. it looks like you may be using C because of your use of sscanf() but i loooked up C output and tried to use it (puts), got that working but apparently there are other differences like with for-loops that i don't know about. but this should get you pointed in the right direction, at least.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Thanks, I'll look into this.

    (I'm using c++ btw.)

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