I need help with fscanf

This is a discussion on I need help with fscanf within the C Programming forums, part of the General Programming Boards category; So I am working on a project and I need to sort numbers. He wants us to sort from a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    I need help with fscanf

    So I am working on a project and I need to sort numbers. He wants us to sort from a file he has only he posted it with the list as 0123456789. Is there anyway I can get fscanf to space the numbers so that i could scan in 0 1 2 3 4 5 6 7 8 9?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Run a loop scanning each number at a time and storing it, in an array perhaps...? Dunno it's your homework not mine.

    http://www.cplusplus.com/reference/c...io/fscanf.html

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    There's no way to get fscanf() to do that when you're reading ints. Unless there's some weird rule for numbers with more than one digit you can read the numbers as characters and then convert them to numbers:
    Code:
    while ( ( digit = fgetc( fp ) ) != EOF ) {
      digit -= '0'; // convert to the matching number
    
      ...
    }

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    > There's no way to get fscanf() to do that when you're reading ints
    Yes there is. Static field widths, for example.

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yeah, I didn't think of that.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As Noir said, its a bit unclear as to how to identify each number from other...

    0123456789 might infact be 9 numbers:
    0123456789

    or 8, or 7, you get the point (anything as low as just one number)... But other than that, Noir's code is suffice.

    You can also use fscanf(), provided you specify the width of the int (eg 1 byte)...

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    excellent thanks guys i didn't think about that. I'm not real sure what my teacher wants with that but i think i'll make it both ways just to make sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 03:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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