Please explain sscanf's %[^=]

This is a discussion on Please explain sscanf's %[^=] within the Linux Programming forums, part of the Platform Specific Boards category; I am not understanding the following line Code: sscanf(string, " %[^=] = %s", name, value); The relative strings come out ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    24

    Please explain sscanf's %[^=]

    I am not understanding the following line
    Code:
            sscanf(string, " %[^=] = %s", name, value);
    The relative strings come out of a file containing
    Code:
    Terminalname = PI1
    version      = 1.0
    serverIP     = 192.168.0.10
    WebService   = CISWebService
    mediaPath    = /home/pi/CIS/MediaManager/Media
    XMLPath      = /home/pi/CIS/MediaManager/Data/XML
    ErrorLog     = /home/pi/CIS/MediaManager/Errors
    timeDelay    = 60
    everything works fine till it processes the line containing mediaPath and instead of seeing MediaPath it sees dia
    Last edited by straygrey; 11-17-2013 at 07:01 AM. Reason: spelling

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Works as expected here.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    int main(void)
    {
      char string[] = "mediaPath    = /home/pi/CIS/MediaManager/Media\n";
      char name[100], value[100];
      sscanf(string, " %[^=] = %s", name, value);
      printf("Input=>>%s<<\n",string);
      printf("Name=>>%s<<\n",name);
      printf("Value=>>%s<<\n",value);
      return 0;
    }
    $ gcc foo.c
    $ ./a.out 
    Input=>>mediaPath    = /home/pi/CIS/MediaManager/Media
    <<
    Name=>>mediaPath    <<
    Value=>>/home/pi/CIS/MediaManager/Media<<
    Check for things like buffer overrun in your assigned results.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    24
    The results I get are :-
    Code:
    Value of variable named Terminalname is set to PI1
    Value of variable named version      is set to 1.0
    Value of variable named serverIP     is set to 192.168.0.10
    Value of variable named WebService   is set to CISWebService
    Value of variable named dia is set to /home/pi/CIS/MediaManager/Media
    Value of variable named ta/XML is set to /home/pi/CIS/MediaManager/Data/XML
    Value of variable named rors is set to /home/pi/CIS/MediaManager/Errors
    Value of variable named timeDelay    is set to 60
    Interesting that name get the value of the last few characters of the value

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by straygrey View Post
    Interesting that name get the value of the last few characters of the value
    Not all that interesting; it just means that you have allocated 28 characters for your strings and, as Salem mentioned above, you are overrunning your buffer.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    24
    Problem solved and thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() help in c
    By ankitsinghal_89 in forum C Programming
    Replies: 2
    Last Post: 01-31-2009, 01:11 PM
  2. sscanf() help
    By cyberfish in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 01:33 PM
  3. sscanf help
    By abulreesh in forum C Programming
    Replies: 3
    Last Post: 12-10-2006, 10:03 AM
  4. sscanf
    By paperbox005 in forum C Programming
    Replies: 2
    Last Post: 08-18-2004, 06:46 AM
  5. sscanf and gets
    By Max in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 07:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21