How Can I Get a File Size in C++? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we often need to determine the size of a file which is the number of bytes in a file. This is done for various applications, such as file processing or validation. In this article, we will learn how to get the file size in C++. Example: Input: FilePath = "C:/Users/Desktop/myFile.txt" Output: 5 Bytes // myFile.txt contains "Hello"How to Get a File's Size in C++?In C++17 and later, we can use the std::filesystem::file_size() function from the std::filesystem library to get the size of a file. This function returns the size of the file in bytes, which can later be used for processing. It is a standalone function so we can call it directly by passing the path of the file as a parameter to the file_size() function. C++ Program to Get File SizeThe below program demonstrates how we can get a file size in C++. C++ // C++ Program to demonstrate how we can get a file // size from a file path #include <filesystem> #include <iostream> using namespace std; int main() { // Define a path object representing the file path. filesystem::path filePath = "C:/Users/Desktop/myFile.txt"; // Print the file size using the file_size() function // of the filesystem library. cout << "File size is: " << filesystem::file_size(filePath) << " bytes" << endl; return 0; } Output File size is: 5 bytesTime Complexity: O(1)Auxiliary Space: O(1) Note: The file_size() function works only in C++17 and later. If the file does not exist, it will throw a filesystem::filesystem_error exception. Comment More infoAdvertise with us Next Article How Can I Get a File Size in C++? D dasrudra0710 Follow Improve Article Tags : C++ Programs C++ cpp-file-handling C++ File Programs CPP Examples misc-cpp +2 More Practice Tags : CPP Similar Reads How to Get File Extension in C++? In C++, we may often find the need to extract the file extension from a given path of the file while working in many applications for processing or validating. In this article, we will learn how to get the file extension in C++. For Example, Input: someFolder â³ filename.ext Output: File Extension = 2 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read How to Get the MD5 Hash of a File in C++? In cryptography, we use the MD5 (Message Digest Algorithm 5) hash function for creating a 128-bit hash value which is represented as a 32-character hexadecimal number. However, this algorithm is not very secure cryptographically but can be used for file verifications, checksums, and ensuring data in 5 min read Like