Thread: need help for file management

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    146

    need help for file management

    Is there any function in any of the c libraries that can read size of the file in bytes ?
    I need to read file size in a program..

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way is to open file, seek to end, get file position, then close.
    Fopen, fseek, ftell, fclose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes
    ftell for example called after fseek(f,0,SEEK_END)

    but note that the return value is only long...

    file should be opend as binary for accurate size calculation.
    MS has some extentions that can handle files with sizes that are bigger than LONG_MAX but if you go with MS extentions you can effectively call some Win32 API that will return actual file size or size on disk
    Last edited by vart; 02-08-2008 at 11:53 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is ftelli64. Don't know if it's a Microsoft extension, though.
    Anyhow, to get the file size of a file through win32 api, you still need to open it through CreateFile which requires a library knowledge itself to use.
    So C functions, even if extensions, are generally easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    I'll try this out..
    Thanks for a quick reply...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There was a thread recently in the Linux Programming Forum about accessing files larger than the range of a long. Look there if you're interested. There are lots of functions for doing it, of various degrees of portability, such as fgetpos()+fsetpos() from C99, and ftello(), etc.

    Anyway, I don't think that's what the OP needs.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management - How/why/when
    By micke_b in forum C Programming
    Replies: 29
    Last Post: 11-07-2007, 12:26 PM
  2. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  3. Memory management
    By CompiledMonkey in forum C++ Programming
    Replies: 9
    Last Post: 12-19-2003, 11:41 AM
  4. FreeBSD User Management
    By frenchfry164 in forum Tech Board
    Replies: 6
    Last Post: 09-11-2003, 01:31 AM
  5. heap memory management
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 10:23 PM