a sscanf question....

This is a discussion on a sscanf question.... within the C Programming forums, part of the General Programming Boards category; hey there, i'm sending strings to another computer via the serial port as I'm developing an interface between two computers ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    a sscanf question....

    hey there,

    i'm sending strings to another computer via the serial port as I'm developing an interface between two computers (one is going to be used onboard an RC helicopter to control it, although its just going to datalog at the moment).

    currently, i'm doing this... :

    rc = sscanf(controlword, "%c, %d, %d", &type, &var_1, &var_2);

    and then testing rc firstly to see if it is 1 or 3, and then type so i can tell it what to do. This works fine for single word entries such as quit... rc=1 and type = q || Q and i don't have to worry about var_1 or var_2.

    If i want to send something with parameters (for example "servo, 1, 255") then for it to be able to get the %d's i have to send it S,1,255.

    I noticed in my copy of kernighan and ritchie that i can use [...] in my sscanf argument type....
    matches the longest non-empty string of input characters from the set between the brackets; char *. A '\0' is added. []...] includes ] in the set.

    but i'm unsure how to use it or whether or not i can specify a string inside the parenthesis. Hopefully this shouldn't be a challenge to all you veterans out there!

    Cheers.
    Dave

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    I think you can try something like this.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char type1[20];
    	char type2;
    	char line[100];
    	int var_1,var_2;
    
    	printf("Enter the line type var1 var2: ");
    	fgets(line,sizeof(line),stdin);
    
    	if (sscanf(line,"%c %d %d",&type2, &var_1, &var_2) == 3 )
    	{
    	     printf("You passed char as type: %c\n", type2);
    	} else if  (sscanf(line,"%s %d %d",type1, &var_1, &var_2) == 3 )
    	{
    	      printf("You passed string as type: %s\n", type1);
    	}
    	
    	return 0;
    }
    When I ran it following was the output.

    [root@linux-outside snoopy]# ./parse
    Enter the line type var1 var2: servo 1 2
    You passed string as type: servo
    [root@linux-outside snoopy]# ./parse
    Enter the line type var1 var2: q 1 2
    You passed char as type: q
    [root@linux-outside snoopy]#

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    5
    cheers for the reply... i managed to sort it out in the end!
    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() question
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-28-2009, 01:57 PM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 05:42 AM
  3. sscanf question
    By Little_Dump in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 01:16 PM
  4. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 08:19 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

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