Just Learn Code

Mastering File Size Retrieval in C++: A Comprehensive Guide

Retrieving File Size in C++: A Comprehensive Guide

File size is a crucial piece of information that programmers need to carry out various operations, such as file transfers, data processing, and memory allocation. The size of a file indicates the number of bytes occupied by its content, and it can be obtained through different mechanisms in C++.

In this guide, we will explore two popular functions that allow us to retrieve the file size: std::filesystem::file_size and stat. We will explain their usage and provide examples to help you understand how to use them effectively.

Using std::filesystem::file_size Function

The std::filesystem::file_size function is part of the C++17 standard library and provides a simple and efficient way to get the size of a file. To use this function, you need to make sure that you include the header file.

The primary syntax for this function is:

std::uintmax_t std::filesystem::file_size(const std::filesystem::path& p);

The std::uintmax_t type guarantees that we can store the maximum file size that the system can handle, and the std::filesystem::path type specifies the path to the file we want to check. Example 1: Retrieving the Size of a Single File

std::filesystem::path filepath = “C:\example.txt”;

auto filesize = std::filesystem::file_size(filepath);

std::cout << "The size of " << filepath << " is " << filesize << " bytes." << std::endl;

In the example above, we created an instance of the std::filesystem::path class with the path to the file we want to retrieve the size “C:\example.txt.” Then, we called the std::filesystem::file_size function with filepath as an argument and stored the result in the filesize variable.

Finally, we printed out a message to show the file size in bytes. Alternative Usage: std::filesystem::path Variable

Instead of creating a std::filesystem::path object explicitly, we can use a string literal to define the file path directly.

Example 2: Using std::filesystem::path to Define the File Path

auto filesize = std::filesystem::file_size(“C:\example.txt”);

std::cout << "The size of the file is " << filesize << " bytes." << std::endl;

This alternative syntax is useful if you only need to retrieve the file size once and don’t need to use the file path as a variable elsewhere in your code.

Usage with error_code Object for Error Handling

The std::filesystem::file_size function can throw an exception if it fails to retrieve the file size. To avoid exceptions, you can use an error_code object to check whether an error occurred and handle it gracefully.

Example 3: Handling Errors with error_code Object

std::error_code ec;

auto filesize = std::filesystem::file_size(“C:\example.txt”, ec);

if (!ec) {

std::cout << "The size of the file is " << filesize << " bytes." << std::endl;


else {

std::cout << "Error " << ec.value() << ": " << ec.message() << std::endl;


In the example above, we created an instance of std::error_code (ec) to check for errors. If the function succeeds, the error_code value is zero, and we can print out the file size as before.

Otherwise, we print out an error message that includes the error code (ec.value()) and the error message (ec.message()).

Using stat Function

The stat function is a C library function that is POSIX-compliant, which means that it is supported by multiple operating systems, including UNIX-based systems, macOS, and Windows Subsystem for Linux. To use this function, we need to include the header file and call it with a character string representing the file path and a pointer to a struct stat variable that contains information about the file.

The syntax is:

int stat(const char* path, struct stat* buf);

The int type indicates whether the function succeeded (return value of 0) or failed (return value of -1), and the struct stat type holds multiple fields that contain file parameters such as size, date modified, and permissions. Example 4: Retrieving the Size of a Single File Using stat

struct stat file_stat;

if (stat(“C:\example.txt”, &file_stat) == 0) {

auto filesize = file_stat.st_size;

std::cout << "The size of the file is " << filesize << " bytes." << std::endl;


else {

std::cout << "Error retrieving file size." << std::endl;


In the above example, we created a struct stat variable file_stat and checked whether the stat function call was successful.

If it was, we assigned the file size stored in the st_size field to the filesize variable and printed it out. Otherwise, we printed out an error message because the function failed.


Retrieving file size is a straightforward operation in C++. The std::filesystem::file_size and stat functions provide different mechanisms for obtaining this information, and the choice depends on your programming needs and the level of system compatibility you require.

Remember to handle exceptions and errors gracefully to ensure the robustness of your application. With this knowledge, you can confidently add file size retrieval to your programming toolkit.

In summary, this guide explores two commonly used functions in C++ for retrieving file sizes: std::filesystem::file_size and stat. While std::filesystem::file_size is simpler and more efficient, stat is POSIX-compliant and can work across multiple operating systems.

Both functions require error-handling mechanisms to ensure robustness and a smooth user experience. By using these functions, programmers can unlock a crucial piece of information that can help them optimize their code and carry out various operations.

Always remember to handle exceptions and errors gracefully. With this knowledge, readers can confidently retrieve file sizes in their code and increase their programming competency.

Popular Posts