Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / MFC
Article

Quick and Dirty Series: C++ FileSize() function

Rate me:
Please Sign up or sign in to vote.
4.08/5 (25 votes)
15 Dec 20042 min read 179.2K   30   18
Finding the size of the file in C++ in a portable manner is not trivial!

Introduction

The C++ standard library doesn't have a FileSize() function, and in fact, there is no simple way to query the file size in a portable manner. To help others avoid the embarrassment of asking such a question on newsgroups, I decided to post a solution.

The reason is that the size of a file is only really accessible through the operating system. C++ was written to assume as little about the platform as possible, including whether or not there is an operating system with a file system. Yeah, I know, my reaction is the same as yours, but nonetheless, we must carry onward brave programmer, and get the job done.

Compiler Specific: Visual C++

The following contribution by Jesse Chisholm works very well if you will only ever be compiling with Visual C++:

#include <sys\types.h> 
#include <sys\stat.h> 
__int64 FileSize64( const char * szFileName ) 
{ 
  struct __stat64 fileStat; 
  int err = _stat64( szFileName, &fileStat ); 
  if (0 != err) return 0; 
  return fileStat.st_size; 
}

Pitfalls:

  • Specific to Visual C++.

Non-Portable Version: stat()

Many C++ compilers provide the C run-time function stat():

#include <sys\types.h> 
#include <sys\stat.h> 
int FileSize( const char * szFileName ) 
{ 
  struct stat fileStat; 
  int err = stat( szFileName, &fileStat ); 
  if (0 != err) return 0; 
  return fileStat.st_size; 
}

Pitfalls:

  • stat() isn't part of the C++ standard, so it may or may not be available.
  • The file size may be bigger than can be represented by an int.
  • stat() isn't necessarily precise.

Portable Version: ifstream::tellg()

The following is kind of a defacto standard I use, and which I have seen often, with variations:

#include <fstream>
int FileSize(const char* sFileName)
{
  std::ifstream f;
  f.open(sFileName, std::ios_base::binary | std::ios_base::in);
  if (!f.good() || f.eof() || !f.is_open()) { return 0; }
  f.seekg(0, std::ios_base::beg);
  std::ifstream::pos_type begin_pos = f.tellg();
  f.seekg(0, std::ios_base::end);
  return static_cast<int>(f.tellg() - begin_pos);
}

Pitfalls:

  • The file size may be bigger than can be represented by an int.
  • The size of the file may be larger than what is reported.

If you can live with that, which I usually can, then great! Otherwise, there is another option.

Using Boost

There is a better solution, if you have the Boost C++ library installed which provides us with the following function:

#include <boost/filesystem/operations.hpp>
boost::intmax_t file_size( const path & ph );

Pitfalls:

  • You need to have Boost.
  • If a compiler does not support maxint_t large enough to represent the operating system's maximum file size, the returned value could be incorrect.

More information on boost::file_size is available here.

Summary

C++ is very hard to master. This is mainly because there are so many ways to do things, it can be hard to identify the best way (or the path of least evil, if you will). As a professional coder, sometimes good enough is in fact good enough, so I have decided to start a series of articles at CodeProject called the Quick and Dirty Series, for coders who need to get a job done, and wouldn't mind learning a thing or two along the way.

Acknowledgements

The following people helped out enormously by responding to my post on comp.lang.c++: Jonathan Turkanis, Siemel Naran, and Thomas Matthews. Jesse Chisholm supplied the Visual C++ version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Ara 3D
Canada Canada
I am the designer of the Plato programming language and I am the founder of Ara 3D. I can be reached via email at cdiggins@gmail.com

Comments and Discussions

 
GeneralA pitfall worth noting Pin
.:floyd:.5-Oct-15 4:10
.:floyd:.5-Oct-15 4:10 
Questionnumber of threads created so far Pin
Vinod.E13-Mar-08 23:35
Vinod.E13-Mar-08 23:35 
QuestionRe: number of threads created so far Pin
David Crow30-May-08 3:32
David Crow30-May-08 3:32 
QuestionIsn't it simpler? Pin
DaisyWheel21-Jun-07 23:51
DaisyWheel21-Jun-07 23:51 
AnswerRe: Isn't it simpler? Pin
Paul Sanders (the other one)20-Oct-07 7:25
Paul Sanders (the other one)20-Oct-07 7:25 
GeneralThank you! Pin
lchung123415-Nov-06 16:24
lchung123415-Nov-06 16:24 
GeneralDefined in ANSI-C Pin
pkpera13-Jun-06 7:17
pkpera13-Jun-06 7:17 
GeneralTried out the Compiler Specific: Visual C++ code Pin
ddas-edEn10-May-05 20:21
ddas-edEn10-May-05 20:21 
Generalon stat Pin
Henry miller15-Dec-04 11:19
Henry miller15-Dec-04 11:19 
GeneralAn alternative: stat() Pin
skst15-Dec-04 6:29
skst15-Dec-04 6:29 
GeneralRe: An alternative: stat() Pin
Christopher Diggins15-Dec-04 7:10
professionalChristopher Diggins15-Dec-04 7:10 
GeneralRe: An alternative: stat() Pin
Neville Franks15-Dec-04 9:15
Neville Franks15-Dec-04 9:15 
GeneralRe: An alternative: stat() Pin
Christopher Diggins15-Dec-04 9:55
professionalChristopher Diggins15-Dec-04 9:55 
GeneralRe: An alternative: stat() Pin
Neville Franks15-Dec-04 10:10
Neville Franks15-Dec-04 10:10 
QuestionWhy not like this? Pin
S.H.Bouwhuis14-Dec-04 9:38
S.H.Bouwhuis14-Dec-04 9:38 
AnswerRe: Why not like this? Pin
wac14-Dec-04 10:41
wac14-Dec-04 10:41 
AnswerRe: Why not like this? Pin
Christopher Diggins14-Dec-04 10:49
professionalChristopher Diggins14-Dec-04 10:49 
That is fine except that it is simply Windows specific, i.e. non-portable.

Christopher Diggins
www.CDiggins.com
www.Heron-Language.com
AnswerRe: Why not like this? Pin
f25-Jul-06 6:59
f25-Jul-06 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.