Archived

This topic is now archived and is closed to further replies.

Gaiiden

file sizes

Recommended Posts

How can you find the size of a file without having to read through it? I''m using a fstream object for file I/O. Thanx. ============================== "Need more eeenput..." - #5, "Short Circuit" ==============================

Share this post


Link to post
Share on other sites
Use rdbuf() to get the filebuf object attached to the fstream. Then you can seek to the end of the file use seekoff() to get the streampos of the end of the file.

Share this post


Link to post
Share on other sites
i use:



int currentPos, size;

currentPos = file.tellg(); //get position of getpointer

file.seekg(0, std::ios::end); //move getpointer to last byte

size = file.tellg(); //get pos of last byte = size

file.seekg(currentPos); //reset the original getpointer



i don''t know if this is the best way of handling it, but it works for me

Share this post


Link to post
Share on other sites
I find the easiest way is to use the stat() routine. (state.h/types.h). Look up in the MSDN or other doc on how to use it. Microsoft of course calls it _stat():

int _stat( const char *path, struct _stat *buffer );


It will give you size, last access date, creation date, and all the other goodies on the file.

CodeMonkey

Share this post


Link to post
Share on other sites
This is a FAQ. (http://www.eskimo.com/~scs/C-faq/q19.12.html) None of the functions listed above are standard (Referring to the ANSI standard here, not POSIX). I would ask if you really need to know the size of the file before-hand. Perhaps you can adjust your code to learn the size of the file as it reads it in.

Share this post


Link to post
Share on other sites