Thread: Help with sscanf with a pipedelimter

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Help with sscanf with a pipedelimter

    char *test="Just Dance 2 by UBI Soft|Nintendo Wii";
    char title[100],platform[100]

    sscanf(test,"%[^|],%s",title,platform);


    printf("%s\n",platform);



    If I print the tile it shows the just dance 2 by ubi soft
    but if i try to print platform its empty

    If I chance it to a comma it works but not with pipe and I have to use pipe

    Any idea why its not working?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    sscanf(test,"%[^|]|%s",title,platform);

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Thanks! I was looking everywhere on examples and couldnt find one.

    Thanks for your help!

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    BillyTKid, don't you think it might have been helpful to explain why your code works?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Well the only difference was the comma replace by the pipe.

    I believe what it does is it scans the string and takes the first part up to the pipe than it uses the pipe as the next starting point. I had a comma there and that is why it failed.

    That is what I assume.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    "%[^|]|%s"

    %[^|] - Reading everything apart of '|' char and bind it to a variable
    | - next char should be '|', if it was successful match just discard it
    %s - Read rest of the char and bind it to a vriable provided

    When you had ',' there is was matching ',' when it was '|' char and match failed the string format and the sscanf function return with just 1 as it binded just one variable but not two.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array or Structs? and using sscanf
    By robin2aj in forum C Programming
    Replies: 3
    Last Post: 04-08-2010, 03:53 PM
  2. sscanf error
    By roaan in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 06:44 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM