0
0
CppHow-ToBeginner · 3 min read

How to Get File Size in C++: Simple Methods Explained

To get a file size in C++, open the file in binary mode and use seekg to move to the end, then use tellg to get the position which equals the file size in bytes. Alternatively, use std::filesystem::file_size from C++17 for a simpler approach.
📐

Syntax

Here are two common ways to get file size in C++:

  • Using file streams: Open the file in binary mode, move the read pointer to the end with seekg, then get the position with tellg.
  • Using std::filesystem (C++17 and later): Call std::filesystem::file_size(path) to get the size directly.
cpp
#include <fstream>

std::ifstream file("filename", std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();

// Or using filesystem
#include <filesystem>
auto size = std::filesystem::file_size("filename");
💻

Example

This example shows how to get the size of a file named example.txt using both methods. It prints the file size in bytes.

cpp
#include <iostream>
#include <fstream>
#include <filesystem>

int main() {
    const char* filename = "example.txt";

    // Method 1: Using ifstream
    std::ifstream file(filename, std::ios::binary | std::ios::ate);
    if (!file) {
        std::cerr << "Failed to open file with ifstream.\n";
        return 1;
    }
    std::streamsize size1 = file.tellg();
    file.close();

    // Method 2: Using std::filesystem
    std::error_code ec;
    auto size2 = std::filesystem::file_size(filename, ec);
    if (ec) {
        std::cerr << "Failed to get file size with filesystem: " << ec.message() << "\n";
        return 1;
    }

    std::cout << "File size using ifstream: " << size1 << " bytes\n";
    std::cout << "File size using filesystem: " << size2 << " bytes\n";

    return 0;
}
Output
File size using ifstream: 1234 bytes File size using filesystem: 1234 bytes
⚠️

Common Pitfalls

Common mistakes when getting file size in C++ include:

  • Not opening the file in binary mode, which can cause incorrect size on some systems.
  • Not opening the file with std::ios::ate, so tellg() does not give the file size.
  • Not checking if the file opened successfully before calling tellg().
  • Using tellg() after reading or writing without resetting the position.
  • Not handling exceptions or errors when using std::filesystem::file_size.
cpp
/* Wrong way: Missing binary and ate flags */
std::ifstream file_wrong("file.txt");
if (file_wrong) {
    auto size_wrong = file_wrong.tellg(); // May return -1 or incorrect size
}

/* Right way: */
std::ifstream file_right("file.txt", std::ios::binary | std::ios::ate);
if (file_right) {
    auto size_right = file_right.tellg();
}
📊

Quick Reference

Summary tips for getting file size in C++:

  • Use std::ifstream with std::ios::binary | std::ios::ate to get size via tellg().
  • Use std::filesystem::file_size for a simpler and safer approach (C++17+).
  • Always check if the file opened successfully before querying size.
  • Handle errors or exceptions when using filesystem functions.

Key Takeaways

Open files in binary mode with std::ios::ate to get accurate size using tellg().
Use std::filesystem::file_size for a modern and straightforward way to get file size.
Always check if the file opened successfully before accessing its size.
Handle errors properly when using filesystem functions to avoid crashes.
Avoid reading the whole file just to find its size; use seekg and tellg or filesystem.