How Can I Get a File Size in C++? Last Updated : 28 May, 2024 Summarize Comments Improve Suggest changes Share 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 to Read a File Line by Line 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 Current Directory in C++ The current working directory, also known as the present working directory, is the location on the file system where the executing program is located and operates from. When working with files and directories in C++, it is important to determine the current working directory to find the resource fil 2 min read How to Open and Close a File in C++? In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst 2 min read Like