Jump to content
  • Advertisement
Sign in to follow this  
personwholives

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
std::size_t size(std::string filename){

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

Share this post


Link to post
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();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!