Thread: sscanf _ problem

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    32

    sscanf _ problem

    hey mates.

    i want to get two variables from a string that changes occasionally.

    Code:
    	if (sscanf(s, "%s_%s", name, id) >= 1)
    But this code never returns the id. How can i do that?

    (the string is like "#guyab_1" "#aden_25" and so on...)

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because the first %s will include _

    Try something like this:
    Code:
    sscanf(s, "%[^_]_%s", name, id)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by MK27 View Post
    Because the first %s will include _

    Try something like this:
    Code:
    sscanf(s, "%[^_]_%s", name, id)
    Oh thank you for the quick reply. It works. But i should ask this. Is this normal if sscanf returns 1 when string like "#z-dot" or something?

  4. #4
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    scanf, fscanf, sscanf ... all of these return the number of arguments successfully parsed.

    since you get the first argument but not the second, the return value is 1 and not 2

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Thank you guys. Much appreciated.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Sorry mates but i am in a dilemma now. So didn't want to create a new thread. Here i go:

    What about space char? I mean

    sscanf(s, "%s %s", name, company) parses a string like 'john nashville' but it also parses 'john nashville ' and so on.. How i can make this to parse only the first string?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    As written, name will be "john" and company will be "nashville" and whatever else remains in buffer s will still be there.

    The separator for %s is whitespace (of all kinds)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    To sum it up you're saying it's not possible with sscanf? Right?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by |HBO| View Post
    To sum it up you're saying it's not possible with sscanf? Right?
    I think we're saying you shouldn't worry about it. Is there a reason you are worried about it? If so, what is that reason?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I've no idea - you've not exactly posted a consistent set of strings to parse.

    If your input is "john nashville IBM", and you're hoping to get "john nashville" into name, and "IBM" into company, then the answer is no.

    But if you've got something else going on, you need to post some examples.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    input: 'john IBM has some secrets'
    get: 'john IBM'

    This is ok.

    But i don't want to sscanf parse when the string goes like:
    input: 'john IBM has some secrets'

    sscanf still does the parsing. I just want to parse a string which is like the first one ''john IBM has some secrets'

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %n will tell you how many characters were read, which you can then compare to strlen to see if there were things left over.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Oh thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  3. sscanf problem
    By LordShme in forum C Programming
    Replies: 5
    Last Post: 12-05-2006, 09:09 PM
  4. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM