Thread: Getting file sizes

  1. #16
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    here you a go, I'm new to C so please show me a better way of this is wrong, thanks.


    int filesize(FILE *handle)
    {
    fseek(handle,0,SEEK_END);
    fsize=ftell(handle);

    return fsize;
    }

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @xoid:
    an int possibly won't be big enough to hold the file size. Besides, ftell() returns a long int.

    Also, fseeking to the end of a file isn't guaranteed to tell you the correct answer to the question. The file open mode can affect the answer too.

    http://www.eskimo.com/~scs/C-faq/q19.12.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    maybe I'm wrong, but if you want to find an exact filesize you need Unix, LOL, I'm using windows, Visual C++ 6 compiler currently learning ANSI C, anything I could use that will guarantee the correct filesize?

    thanks in advance

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use a binary open mode in your fopen() call. Then you can fseek()/ftell()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    cool, is that the most efficient method under windows?

  6. #21
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yep.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Xoid
    cool, is that the most efficient method under windows?
    You may also want to look up _stat() in MSDN or GetFileSize()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    aah! many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM