Advertisement

file sizes

Started by October 29, 2000 01:36 PM
5 comments, last by Gaiiden 24 years, 2 months ago
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" ==============================

Drew Sikora
Executive Producer
GameDev.net

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.
Advertisement
i use:

int currentPos, size;	currentPos = file.tellg();	//get position of getpointer		file.seekg(0, std::ios::end);	//move getpointer to last bytesize = 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
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
CODE Monkey
thanks all. Can I use this _stat() without MFC?

==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Drew Sikora
Executive Producer
GameDev.net

Yep you can use it without MFC (stat() is in the POSIX standard btw).
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
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.

This topic is closed to new replies.

Advertisement