file size

This is a discussion on file size within the C Programming forums, part of the General Programming Boards category; Hello everyone, thanks in advance for any help. Is there any way to easily calculate a file's size? I don't ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    1

    file size

    Hello everyone, thanks in advance for any help.

    Is there any way to easily calculate a file's size? I don't believe there is a simple function to accomplish this, but I could be wrong.

    I was thinking something along the lines of using a stream and iterating through it.

    Any help would be much appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    there are several functions
    1. stat()
    2. open the file, seek to end of file, then get current file pointer position
    3. MS-Windows -- GetFileSize().

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Indeed, open the file (fopen), seek to end of file (fseek), get current file position (ftell) is the only way with standard C that I can think of.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    That's how I do it.
    seek to end of file (fseek), get current file position (ftell) . . .
    Or fsetpos() and fgetpos(), in C99.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    stat() function is also standard C

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    stat() function is also standard C
    No it isn't.
    Quote Originally Posted by dwks
    Or fsetpos() and fgetpos(), in C99.
    You can't use fgetpos() to determine the size, because fpos_t isn't necessarily an integer value.
    Last edited by cwr; 09-19-2005 at 09:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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