sscanf read int from strings of "image_00021"

This is a discussion on sscanf read int from strings of "image_00021" within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have a database of files that I am reading in and working on with the titles created ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Smile sscanf read int from strings of "image_00021"

    Hi all,

    I have a database of files that I am reading in and working on with the titles created by:
    Code:
    ...
    ugv++;
    sprintf(input_file, "UGV_%i/image_%05d.jpg", ugv, img_num)
    ...

    This creates a list of files like this, which I need to be ordered so I can sort the files easily in the folder:
    image_00001.jpg
    image_00002.jpg
    ...
    image_00009.jpg
    image_00010.jpg
    image_00011.jpg

    I can read in the titles of these files easily enough, but because the integers I want (1,2,...9,10,11) are preceded by an unknown number of zeroes, reading in the desired number is much more difficult than if you have a known position of an integer.

    My code currently is:
    Code:
    int latestImgNo;
    sscanf(imgString, "%*c%05d", latestImgNo);
    But I get the warning:
    warning: format '%05d' expects type 'int*', but argument 3 has type 'int'

    I have tried making latestImgNo a pointer, but it gives the same sorts of errors.

    I have tried looking up delimeters, but that doesn't really help (as far as I know) because I don't know how many zeroes to search past.

    Should I use something to convert "00021" as a string to an integer, that would just ignore zeroes?

    Thanks for any suggestions

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Thumbs up Solved!

    To think it was this easy...

    My problems were:

    1. sscanf is a function! Therefore, quoting from :
    "These arguments are expected to be references (pointers): if you want to store the result of a sscanf operation on a regular variable you should precede its identifier with the reference operator, i.e. an ampersand sign (&)". So I added an & before latestImgNo.

    2. I needed to understand the modifier part of sscanf()

    sscanf = %[*][width][modifiers]type

    From http://www.opengroup.org/onlinepubs/...ons/scanf.html

    Each conversion specification is introduced by the character '%' or the character sequence "%n$", after which the following appear in sequence:
    • An optional assignment-suppressing character '*'.
    • An optional non-zero decimal integer that specifies the maximum field width.
    • An option length modifier that specifies the size of the receiving object.
    • A conversion specifier character that specifies the type of conversion to be applied.

    So that's it. I needed %5d not %05d as you need with sprintf(), and %*6c not %*c which I think just read the first character (not sure on this). The '*' as above in the first dot point means you read the character/int/whatever, but don't assign it to a variable

    So the code that worked was

    Code:
    int latestImgNo;
    sscanf(imgString, "%*6c%5d", &latestImgNo);
    Hopefully that helps others!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

Tags for this Thread


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