Thread: sscanf and parsing strings

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    sscanf and parsing strings

    Hi all,

    This is my first post here so I don't know if it is a repost but I would appreciate any replies even if it is. I am writing a small command interpreter and after getting a line of input from the user I have to parse it to figure out which command was given. What I have now is as follows:

    Code:
    //Inside loop getting info from the user
    char *infoa;
    char *infob;
    
    if (sscanf(line, "command option %s %s", infoa, infob) == 2){
       //Do the command
    }
    There are then other sscanf if statements to check the other commands. The first issue is this is giving me a bus error when i type "command option a b" (a and b can be anything). If I change the %s to %d and the type of infoa and infob to ints, it works so why is it not working with strings? Secondly, I believe there must be a better way to parse strings but I have no idea what it is. If anyone can point me in a better direction, I would definately appreciate it.

    Thanks all

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    >If anyone can point me in a better direction
    pointing direction
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Your pointers don't point anywhere.

    Make them arrays or allocate some memory and make them point there.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'll have to point you in a cautionary direction: http://cboard.cprogramming.com/showthread.php?t=56248

    In other words, you can't store anything in a pointer that doesn't point to anything. You have to make it point somewhere -- by using dynamic memory allocation -- or use an array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    thanks for the responses. they were very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing using strtok() and sscanf()
    By NuNn in forum C Programming
    Replies: 13
    Last Post: 02-12-2009, 02:43 PM
  2. Some questions on sscanf and parsing data
    By green2black in forum C Programming
    Replies: 7
    Last Post: 12-02-2008, 08:25 PM
  3. Replies: 12
    Last Post: 10-16-2008, 12:07 PM
  4. sscanf and parsing invalid floats?
    By Axel in forum C Programming
    Replies: 7
    Last Post: 10-20-2006, 06:36 AM
  5. Parsing a line using sscanf()
    By caduardo21 in forum C Programming
    Replies: 6
    Last Post: 02-10-2005, 07:38 AM