Results 1 to 12 of 12

Thread: Why this is an endless loop?

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Why this is an endless loop?

    Simple:

    I want to get the size of a file but the following code loops forever and freezes my application:


    Code:
    FILE *file;
    int filesize;
    
    while(!feof(file))
    	{
    			
    	       filesize++;
    	}

    What is wrong with this code and why does it freeze?
    Baaaaaaaaah

  2. #2
    Zaei
    Guest
    Because you arent doing anything to the file. You are basically doing this:
    Code:
    for(INT fileSize = 0; ; ++fileSize);
    There is no condition to end the loop, because you arent seeking the file.

    Z.

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    So how can I get the file size using C functions - not API
    Baaaaaaaaah

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Zaei
    Guest
    Very likely, there is a function that will retrieve a files attributes. Look around in the MSDN.

    Z.

  6. #6
    Zaei
    Guest
    Grrrr... =).

    Z.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    heheh
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    How come MSDN has the resource for general C\C++ programming language
    Baaaaaaaaah

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It doesn't have the resource, it's just a resource available. It just happens to be a very useful resource though, especially if you're using Windows.

    You'll need to actually open a file before you can get its attributes, currently you're just declaring a pointer to a file. The standard C way to do that is using fopen().
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can also use _stat to get the file's information. Search for stat.c and that will give an example
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by HarryW


    You'll need to actually open a file before you can get its attributes, currently you're just declaring a pointer to a file. The standard C way to do that is using fopen().
    Sorry, I just wrote the code in post. I had already opened the file using "fopen" command
    Baaaaaaaaah

  12. #12
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    try something like GetFileAttributes{API though}
    Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width