You can open the file using the ios::ate
flag (and ios::binary
flag), so the tellg()
function will give you directly the file size:
ifstream file( "example.txt", ios::binary | ios::ate);
return file.tellg();
More Related Contents:
- c++ open file in one line
- Assigning char array from ifstream breaks array if adding another char to array
- Why does std::getline() skip input after a formatted extraction?
- Reading from text file until EOF repeats last line [duplicate]
- How to construct a c++ fstream from a POSIX file descriptor?
- mmap() vs. reading blocks
- Getting a FILE* from a std::fstream
- How can I get a file’s size in C++? [duplicate]
- How to read a file line by line or a whole text file at once?
- std::fstream doesn’t create file
- Why istream object can be used as a bool expression?
- How to read a growing text file in C++?
- C++ Filehandling: Difference between ios::app and ios::ate?
- Retrieving file descriptor from a std::fstream [duplicate]
- do I need to close a std::fstream? [duplicate]
- How to write to middle of a file in C++?
- Why is failbit set when eof is found on read?
- Get an istream from a char*
- Reading and writing to the same file using the same fstream
- fstream won’t create a file [duplicate]
- How to truncate a file while it is open with fstream
- Reading line from text file and putting the strings into a vector?
- What’s the difference between opening a file with ios::binary or ios::out or both?
- C++ streams confusion: istreambuf_iterator vs istream_iterator?
- How to count lines of a file in C++?
- std::ofstream, check if file exists before writing
- CLion C++ can’t read/open .txt file in project directory
- Why can’t I move std::ofstream?
- What’s the difference between getline and std::istream::operator>>()?
- are “seekp” & “seekg” interchangeable?