# Getting file size

This topic is 5475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on a program and i was trying to get the size of files (without having to open them, preferably). I have found (on the msdn site) an _stat() function, and a file length function (that requires one be using windows goofy file search method for finding files). neither of these really fit what i want. There is also a way to get the file length once the file is opened using fopen() and while this would work, i was hoping there might be another way. Anyway, if anyone knows anything that might help me, it would be apreciated. Thanks personwholives

##### Share on other sites
As far as I know, theres no way to get the size of a file without opening it. If you're using the Win32 file I/O functions, use GetFileSize(). If you're using the C style file I/O, either use filelength(fileno(pFile)), or the portable way, which is to fseek() to the end of the file, then ftell().

EDIT: Apparently GetFileAttributesEx can get the size of a file (among other things) from just a filename:
DWORD GetFileSize(LPCSTR szFilename){WIN32_FILE_ATTRIBUTE_DATA theData;   GetFileAttributesEx(szFilename,GetFileExInfoStandard,(void*)&theData);   return theData.nFileSizeLow;}

##### Share on other sites
Quote:
 Original post by Evil Steve...or the portable way, which is to fseek() to the end of the file, then ftell().

This isn't portable at all. fseek and ftell won't return the number of bytes in the file if the files have been opened in text mode, and thus have CR/LF translation (on windows).

##### Share on other sites
Just use stat(). This is a C function and I am pretty sure its cross platform. Visual C++ 6.0 and lower called this function _stat().

##### Share on other sites
Quote:
Original post by Washu
Quote:
 Original post by Evil Steve...or the portable way, which is to fseek() to the end of the file, then ftell().

This isn't portable at all. fseek and ftell won't return the number of bytes in the file if the files have been opened in text mode, and thus have CR/LF translation (on windows).

Well, just open the file in binary mode :P When I said it was portable, I meant its ANSI C, unlike the filelength() function, which is MSVC only.

##### Share on other sites
Stat should do what you want.

##### Share on other sites
On windows, the _stat function returns the lower 32 bits of the size in "st_size" element of struct _stat.
There is some other _stat (like _stat64, I think) that returns the size as a 64 bit number).
This way you can go over 2 gig limit of _stat function.

##### Share on other sites
std::size_t size(std::string filename){std::ifstream file(filename.c_str(), std::ios::binary);std::stringstream ss;ss << file.rdbuf();return ss.str().size();}

##### Share on other sites
std::size_t size(std::string filename){

std::ifstream file(filename.c_str(), std::ios::binary);
//std::stringstream ss;
//ss

##### Share on other sites
the heck? it ate my code, and posted as annonymous ;_;. (^^^ == me)

std::size_t size(std::string filename){    std::ifstream file(filename.c_str(), std::ios::binary);    //REMOVED: reimplemented    //std::stringstream ss;    //ss << file.rdbuf();    //return ss.str().size();    file.seekg( 0 , std::ios::end );    return file.tellg();}

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46