Thread: Using SSCANF to get a variable

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Using SSCANF to get a variable

    I have a string str[80]="Hello #YourName#, this is an example"
    I need a function to determine if str contains a variable pattern like: #YourName# (pattern is defined between the '#' characters). If str contains the pattern, return YourName
    I know sscanf is used to read memory to variable.

    How can I use SSCANF to get this variable?

    Thank you.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's my first guess:
    Code:
    sscanf( str, "Hello #%s#, this is an example", name );
    If the text before & after the #%s# can change too, you might need to break it up somehow or do this (but at the risk of a buffer overflow):
    Code:
    char garbage1[256];
    char garbage2[256];
    sscanf( str, "%s#%s#%s", garbage1, name, garbage2 );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I would use strchr to locate first and second '#', and then strncpy

    but this only gives a string plcaed between two #, I do not see a way to use the variable with that name - you'll need to implement some map container
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Code:
    int main(void){
           char str[80]="Hello #YourName#, this is an example";       
           char name[50]; 
           sscanf( str, "Hello #%s#, this is an example", name );
           printf("Name is: %s", name); 
           system("pause");  
           return 0;
    }
    Output:
    Code:
    Name is: YourName#,
    If I use: sscanf( str, "%s#%s#%s", garbage1, name, garbage2 );
    Output:
    Code:
    Name is:    "
    I think I have to use some thing like: sscanf (%s[....], name) , but i can't figure out how to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM