Get File Size in C++
-
Use the
std::filesystem::file_size
Function to Get File Size in C++ -
Use
std::filesystem::file_size
Withstd::filesystem::path
to Get File Size in C++ -
Use
std::filesystem::file_size
Witherror_code
to Get File Size in C++ -
Use
stat
Function to Get File Size in C++
This article will demonstrate multiple methods of how to get file size in C++.
Use the std::filesystem::file_size
Function to Get File Size in C++
std::filesystem::file_size
is the C++ filesystem library function that retrieves the size of the file in bytes. std::filesystem::file_size
takes path of the file as the function argument, which is of type std::filesystem::path
. In the following example, though, we pass the string
value of the file path, which is then used to construct the corresponding object and retrieve the file size.
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
using std::cout; using std::cin;
using std::endl; using std::ifstream;
int main() {
string path = "input.txt";
cout << "size of file '" << path << "' = " <<
std::filesystem::file_size(path) << endl;
return EXIT_SUCCESS;
}
Output:
size of file 'inbox.txt' = 10440
Use std::filesystem::file_size
With std::filesystem::path
to Get File Size in C++
Alternative usage of std::filesystem::file_size
function is to insert the file path value as the path
type variable. At first, you would need to include the using std::filesystem::path
statement before using the type. Next, we can declare the path
object, as shown in the following example code, and initialize it with a string literal. The constructed variable is passed to the file_size
function to get the file size.
#include <iostream>
#include <string>
#include <fstream>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <filesystem>
using std::cout; using std::cin;
using std::endl; using std::ifstream;
using std::string; using std::filesystem::path;
using std::error_code;
int main() {
class path fd_path = "input.txt";
cout << "size of file '" << fd_path << "' = " <<
std::filesystem::file_size(fd_path) << endl;
return EXIT_SUCCESS;
}
Output:
size of file 'inbox.txt' = 10440
Use std::filesystem::file_size
With error_code
to Get File Size in C++
Since std::filesystem::file_size
utilizes underlying operating system services to retrieve the size of the file, there are good chances it may fail, raising some exception. To better handle each file_size
function call, it is recommended to use the error_code
object as the second argument and store the error message in case of failure. Consequently, we can evaluate the error_code
object and implement error handling code using if..else
statements.
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
using std::cout; using std::cin;
using std::endl; using std::ifstream;
using std::string; using std::filesystem::path;
using std::error_code;
int main() {
string path = "input.txt";
error_code ec{};
auto size = std::filesystem::file_size(path, ec);
if (ec == error_code{})
cout << "size of file '" << path << "' = " << size << endl;
else
cout << "Error accessing file '" << path
<< "' message: " << ec.message() << endl;
return EXIT_SUCCESS;
}
Output:
size of file 'inbox.txt' = 10440
Use stat
Function to Get File Size in C++
stat
is the POSIX compliant function that is available across multiple operating systems. It’s often the function that the std::filesystem::file_size
method underneath the hood. stat
takes character string as the first argument to denote the path of the file address of struct stat
type object as the second argument.
struct stat
is a special predefined object that stores multiple file parameters after the successful stat
function call. Note that we only need to access the st_size
data member of the struct stat
object, which stores file size as a number of bytes.
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
using std::cout; using std::cin;
using std::endl; using std::ifstream;
using std::string;
int main() {
string path = "input.txt";
struct stat sb{};
if (!stat(path.c_str(), &sb)) {
cout << "size of file '" << path << "' = "
<< sb.st_size << endl;
} else {
perror("stat");
}
return EXIT_SUCCESS;
}
Output:
size of file 'inbox.txt' = 10440