sscanf question

This is a discussion on sscanf question within the C Programming forums, part of the General Programming Boards category; Hi All, I wonder how sscanf can take the width of a string as a vaiable. For example, in the ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    14

    Question sscanf question

    Hi All,

    I wonder how sscanf can take the width of a string as a vaiable. For example, in the following sscanf function call:

    sscanf(buf, "%s", name);

    if name is defined as char name[SIZE], how can we put SIZE in the above sscanf call , instead of putting an integer value?

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    A quick search brings up thsi nice solution here
    Quote Originally Posted by http://wiki.answers.com/Q/Can_you_specify_variable_field_width_in_a_scanf_fo rmat_string_If_possible_how
    You can't specify a variable field with a fixed format string, but you can get around this by making the format string variable:
    Code:
     int width;
     char format[20];  /* or whatever size is appropriate */
     int value;
    
     ...
     sprintf(format, "%%%dd", width); /* generates a string like "%5d" */
     scanf(format, &value);
    This example is for "scanf", but of course can be applied to "sscanf".

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: sscanf question

    yeah you can give like,

    Code:
    sprintf(format, "%%%ds", SIZE);
    sscanf(buf, format, name);
    If you didn't specify the length also name will store only SIZE no. of characters.
    If free location is there it will point remaining characters also. So that you can view the all characters while printing that string.
    Last edited by sganesh; 03-05-2010 at 09:36 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > sprintf(format, "%%%ds", SIZE);
    It's not quite so simple...
    Code:
    #include <stdio.h>
    #include <string.h>
    int main ( ) {
      char  buff[20];
      scanf( "%10s", buff );
      printf( "Buff=%s, len=%d\n", buff, strlen(buff) );
      return 0;
    }
    Code:
    $ gcc foo.c
    $ ./a.out 
    this_string_is_longer_than_the_buffer
    Buff=this_strin, len=10
    If you simply use the buffer size, then that is how many characters will be read - EXCLUDING the \0.
    The \0 would go beyond the size and result in a buffer overflow.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    So I can just do the below:

    sprintf(format, "%%%ds", SIZE-1);
    sscanf(buf, format, name);

    Thanks everyone!

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