Using SSCANF to get a variable

This is a discussion on Using SSCANF to get a variable within the C Programming forums, part of the General Programming Boards category; I have a string str[80]="Hello #YourName#, this is an example" I need a function to determine if str contains a ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 01: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, 04:31 PM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02: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

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