#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    [C] printing integers from file


    I want to print integers from file. If I know how many integers will be in a file, let's say 10, i would write a simple loop, like this:

    Code:
      for(i = 0;  i < 10;  i++){
            scanf("%d",&i);
            printf("%d ",i);
        }
    but what to do if i don't know how many integers will be in a file?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Read a number.
    If that succeeds, print it and loop back
    Otherwise you can assume the data has ended and proceed with the rest of the program.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thanks. But how do i check is it succeeded to read a number?

    Code:
        for (i = 1; i < 2; i++){
            scanf("%d",&n);
            if (/* number found */){
                printf("%d ",n);
                i--;
            }
        }
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    if ( scanf("%d", &n ) == 1 )

    But beware that typing in say 123456789012345678901234567890 will generate numeric overflow, but scanf will still claim success and give you some modulo-n result.

    If this is a problem, use fgets() to read a line of input, then strtol() to convert from a string to a long integer. This also sets errno in case of overflow (of a long), then you can range-check it for an integer (or whatever the valid range is).
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thanks, works like a charm.

    But now i'm a bit confused how comparison in C works. If a number is not 1, but say 3, then '3 == 1' should be false, because 3 is not equal 1. So 'if' sentece shouldn't work. But it works :)
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    I think you mis-understand what scanf returns.

    if ( scanf("%d %d", &x, &y ) == 2 )
    The 2 refers to the fact that there are two conversions in the format string, and that both of them were successful - from say typing in 123 456.

    But if you typed in 123 abc, then scanf() would return 1 (assigning 123 to x, and leaving y undefined).
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Thumbs up


    Ohh, i got it now.

IMN logo majestic logo threadwatch logo seochat tools logo