Thread: Problem understanding the working of sscanf()

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    11

    Problem understanding the working of sscanf()

    Hi,

    I am trying to use sscanf() inside a for() loop to read integers into an array, as shown below.

    Code:
    
    char numString[100];    /* Numbers supplied by user. */
    int number[6] = { 0, 0, 0, 0, 0, 0 };
    
    /* Prompt user for input values. */
    printf("Enter a series of six numbers:\n");
            
    /* Capture user number string. */
    fgets(numString, sizeof(numString), stdin);
    
    /* Convert digit string to number and store in array. */
    for (int i = 0; i < 6; ++i)
    {
        sscanf(numString, "%d", &number[i]);
    }
            
    for (int i = 0; i < 6; i++)
    {
        printf("The number is %d\n", number[i]);
    }
    When I run the above code I get the following output

    Enter a series of six numbers:
    123 -254 -300 4096 5 -60000
    The number is 123
    The number is 123
    The number is 123
    The number is 123
    The number is 123
    The number is 123

    From the above output it would seem that each call to sscanf() in the first for() loop causes sscanf() to start reading from the beginning of the number string entered by the user.

    My problem is that I don't understand how to get sscanf() in the first for() loop to advance to the next digit string within the input string. For example, to move from reading 123 to reading -254, and so on. I have(!) done a web search but cannot find anything relating to this particular problem.

    (I know that reading integers into an array can be done using scanf() but I remember reading somewhere that scanf() was either not secure or buggy and so should not be used.)

    Could some kind soul please provide (or point me towards) a text description of how I can get sscanf() to do this advancing ?

    Best regards,

    Stuart

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    From the above output it would seem that each call to sscanf() in the first for() loop causes sscanf() to start reading from the beginning of the number string entered by the user.
    That is correct.

    My problem is that I don't understand how to get sscanf() in the first for() loop to advance to the next digit string within the input string.
    Basically you don't. If you want to get multiple values from sscanf() you need to do it by adding specifiers and variables to place the information into.
    Code:
    sscanf("%d%d", variable[0], variable[1]);
    (I know that reading integers into an array can be done using scanf() but I remember reading somewhere that scanf() was either not secure or buggy and so should not be used.)
    Do you realize that when talking about the "deficiencies" of scanf() you're normally talking about the scanf() series of functions, scanf(), sscanf(), vscanf(), etc?

    Could some kind soul please provide (or point me towards) a text description of how I can get sscanf() to do this advancing ?
    You can't get sscanf() to advance, however you maybe able to "advance" the string by "removing" the first item by using something like strtok(). However this is, IMO, even more error prone than the basic well defined function scanf().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you want to walk through the string with sscanf, then something like this.
    Code:
      /* Convert digit string to number and store in array. */
      char *p = numString;
      int pos;
      for (int i = 0; i < 6; ++i)
      {
          sscanf(p, "%d%n", &number[i],&pos);
          p += pos;
      }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf not working with %s
    By tindala in forum C Programming
    Replies: 4
    Last Post: 04-01-2014, 01:49 PM
  2. understanding sscanf() function
    By ak47 in forum C Programming
    Replies: 6
    Last Post: 01-03-2014, 11:50 PM
  3. sscanf problem
    By baxy in forum C Programming
    Replies: 2
    Last Post: 04-21-2013, 07:45 AM
  4. Replies: 3
    Last Post: 03-27-2010, 04:01 PM
  5. Replies: 4
    Last Post: 10-16-2008, 07:30 PM

Tags for this Thread