Thread: Why does my while >0 go past it into negative numbers?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    27

    Why does my while >0 go past it into negative numbers?

    Code:
     #include   int main () {      FILE * pfile;     unsigned long long bytesremaining =0;     unsigned long long remain = 0;     char name[150] = "";     printf("enter the file name and path\n");     gets(name);      pfile = fopen (name,"rb");      if (pfile==NULL)         perror ("Error opening file");     else     {         fseeko64 (pfile, 0, SEEK_END);         remain = ftello64 (pfile);         bytesremaining=remain;         printf(" bytes left =%I64d\n",bytesremaining);         fclose (pfile);         printf ("\nFile:\n%s\ncontains %I64d bytes:\n", name, remain);         while (bytesremaining >0)         {             printf("bytes remaining = %I64d\n" , bytesremaining);             bytesremaining =bytesremaining-8;          }         break;     }     return 0; }
    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    it is not letting me edit my post it should be
    Code:
     #include   int main () {      FILE * pfile;     unsigned long long bytesremaining =0;     unsigned long long remain = 0;     char name[150] = "";     printf("enter the file name and path\n");     gets(name);      pfile = fopen (name,"rb");      if (pfile==NULL)         perror ("Error opening file");     else     {         fseeko64 (pfile, 0, SEEK_END);         remain = ftello64 (pfile);         bytesremaining=remain;         printf(" bytes left =%I64d\n",bytesremaining);         fclose (pfile);         printf ("\nFile:\n%s\ncontains %I64d bytes:\n", name, remain);         while (bytesremaining >0)         {             printf("bytes remaining = %I64d\n" , bytesremaining);             bytesremaining =bytesremaining-8;          }         break;     }     return 0; }

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Why is this not working?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    I cannot edit my posts or reply(just quick reply)

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Unless bytesremaining is a multiple of 8 such that it could reach 0 exactly, wrapping the unsigned value is practically guaranteed.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Technically printf requires the type to match the format string, so "%I64" should be "%U64". So it's not that bytesremaining is negative, you're just interpreting the very large integer it wraps around to as if it were negative.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Thanks you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Negative Numbers
    By r_james14 in forum C Programming
    Replies: 12
    Last Post: 12-20-2011, 03:01 AM
  2. bit representation of negative numbers
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-26-2009, 10:55 AM
  3. Negative numbers in C
    By BlaX in forum C Programming
    Replies: 18
    Last Post: 06-29-2009, 06:30 PM
  4. Negative Numbers
    By cgod in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2005, 08:57 AM
  5. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM