I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istream
class from the fstream
header?
You can seek until the end, then compute the difference:
std::streampos fileSize( const char* filePath ){
std::streampos fsize = 0;
std::ifstream file( filePath, std::ios::binary );
fsize = file.tellg();
file.seekg( 0, std::ios::end );
fsize = file.tellg() - fsize;
file.close();
return fsize;
}
tellg
not guaranteed to return 0? –
Rumor file.tellg()
after the seekg()
gives the same byte size as is reported by the shell (running on CentOS 4 with g++ 3.4.6) –
Reggi You can open the file using the ios::ate
flag (and ios::binary
flag), so the tellg()
function will directly give you directly the file size:
ifstream file( "example.txt", ios::binary | ios::ate);
return file.tellg();
tellg()
to detect filesize. –
Travis You can seek until the end, then compute the difference:
std::streampos fileSize( const char* filePath ){
std::streampos fsize = 0;
std::ifstream file( filePath, std::ios::binary );
fsize = file.tellg();
file.seekg( 0, std::ios::end );
fsize = file.tellg() - fsize;
file.close();
return fsize;
}
tellg
not guaranteed to return 0? –
Rumor file.tellg()
after the seekg()
gives the same byte size as is reported by the shell (running on CentOS 4 with g++ 3.4.6) –
Reggi Don't use tellg
to determine the exact size of the file. The length determined by tellg
will be larger than the number of characters can be read from the file.
From stackoverflow question tellg() function give wrong size of file? tellg
does not report the size of the file, nor the offset from the beginning in bytes. It reports a token value which can later be used to seek to the same place, and nothing more. (It's not even guaranteed that you can convert the type to an integral type.). For Windows (and most non-Unix systems), in text mode, there is no direct and immediate mapping between what tellg returns and the number of bytes you must read to get to that position.
If it is important to know exactly how many bytes you can read, the only way of reliably doing so is by reading. You should be able to do this with something like:
#include <fstream>
#include <limits>
ifstream file;
file.open(name,std::ios::in|std::ios::binary);
file.ignore( std::numeric_limits<std::streamsize>::max() );
std::streamsize length = file.gcount();
file.clear(); // Since ignore will have set eof.
file.seekg( 0, std::ios_base::beg );
stat()
. –
Burseraceous Like this:
long begin, end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size: " << (end-begin) << " bytes." << endl;
std::streampos
instead of long
as the latter may not support as large a range as the former - and streampos
is more than just an integer. –
Anarch begin
just 0? –
Hodgson Since C++17, we have std::filesystem::file_size
. This doesn't strictly speaking use istream
or fstream
but is by far the most concise and correct way to read a file's size in standard C++.
#include <filesystem>
...
auto size = std::filesystem::file_size("example.txt");
I'm a novice, but this is my self taught way of doing it:
ifstream input_file("example.txt", ios::in | ios::binary)
streambuf* buf_ptr = input_file.rdbuf(); //pointer to the stream buffer
input.get(); //extract one char from the stream, to activate the buffer
input.unget(); //put the character back to undo the get()
size_t file_size = buf_ptr->in_avail();
//a value of 0 will be returned if the stream was not activated, per line 3.
© 2022 - 2024 — McMap. All rights reserved.