Jump to content
  • Advertisement

Archived

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

Shmiznac

Getting File Size in C++

This topic is 5586 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

Do the fstream classes include a way to get the size of a binary file? Or do I have to use the standard _filelength or stat C functions?

Share this post


Link to post
Share on other sites
Advertisement
No, they don't. Theoretically, you don't need them, since you can load the whole file in a dynamic array (vector) without knowing the size in advance.


    
#include <fstream>

#include <vector>

#include <iterator>


using namespace std;

ifstream ifs( "filename", ios::binary );
vector<char> data( istreambuf_iterator<char>( ifs ),
istreambuf_iterator<char>() );

size_t length = data.size();


Disadvantage here if you just wanted to check the file size is that you need to load it first, which not only is costly, but may also be impossible due to read permission issues.

And streams, just like files, don't necessary have a size (e.g. sockets, pipes, and special device files like /dev/urandom or /dev/zero).

It is possible to use seekg/tellg just like fseek/ftell, but the results are highly unreliable - usually due to newline translation, but all the manuals I have explicitely state that the value returned by tell-like functions are only meaningful for seek-like functions, not as numeric values.

Oh, and watch out, stat() is POSIX standard, not ISO C.
And I don't know of a _filelength function;
To my knowledge no standard function starts with an underscore, they usually are extensions.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on March 8, 2003 2:32:46 AM]

Share this post


Link to post
Share on other sites
Hmm...nice use of a vector there, didn''t know you could do that. Funny how I really dislike the C++ file I/O compared to the C version. Anyway, thanks for the info.

Share this post


Link to post
Share on other sites
you can easily use stdio in C++ also, you don''t have to use iostream. i MUCH prefer fopen/fread/fwrite/fclose to fstream. just include stdio.h

Share this post


Link to post
Share on other sites
Yeah I use the C file functions a lot, but when you''re supposed to be programming in C++ it just feels so...dirty . It''s a shame fstream sucks so much.

Share this post


Link to post
Share on other sites
I haven''t tested this myself, just coped it from a site off the net for ya (not at my linux machine so I can''t compile it, and I am too tired to debug it ) - I am sure you can work it out.


// obtaining file size
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
long l,m;
ifstream file (filename, ios::in|ios::binary);
l = file.tellg();
file.seekg (0, ios::end);
m = file.tellg();
file.close();
cout << "size of " << filename;
cout << " is " << (m-l) << " bytes.\n";
return 0;
}

Share this post


Link to post
Share on other sites
Yeah, just seek to the end, then tell current position. That will be file size in bytes.

But yeah, Im sure there is probably some OS call that would tell you...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!