To "check if file is empty in C++":
- Use the fstream for working with files in cpp.
- Write a function isFileEmpty with a return type of void and it is going to take only one string parameter.
- Finally, inside the main method, pass the file path to the isFileEmpty function and it's going to tell us whether the file is empty or not.
Check if a File in Empty in C++
Step 1: Development Environment: What Are You Going to Need?
You can use an IDE installed locally on your computer like
Visual Studio Code because it comes with a lot of useful features like a debugger and code suggestions but for this you can also use
any online C++ compiler like OnlineGDB or Programiz. If you are using a locally
installed IDE or a Code Editior, make sure it has the fstream library installed.
Step 2: Check Empty File: How to Check For an Empty File in C++
Let's start by including all the necessay libraries. Make sure to include this part at the top of the code.
#include <iostream>;
#include <fstream>;
using namespace std;
The iostream library is used for the standard input &
output operations in C++ and the fstream library is used for working with files and directories.
Lastly, the using namespace std; part is used for allowing you to use standard C++ library
components without needing the std:: prefix.
If you are not going to use using namespace std; in your code, you will have to use the std:: prefex before using standard C++ library components.
Writing the isFileEmpty Function
Let's start working on the isFileEmpty function with a return type of void. This function should only take one string parameter.
void isFileEmpty(string filePath) {
ifstream file(filePath);
if (file.is_open()) {
file.seekg(0, ios::end);
int filePointerLocation = file.tellg();
if (filePointerLocation == 0)
cout << "Nothing in this file!" << endl;
else
cout << "This file has content!" << endl;
file.close();
} else {
cout << "Unable to open the file." << endl;
}
}
In this function, we are taking a single string paramater filepath. This variable is going to store the path to our text file that we are going to check.
Moving inside the function, first we got this line of code:
ifstream file(filePath);
What this line of code does is that it takes a file path as a string
parameter and then opens the file for any read or write operations.
After that we got an if statement that is checking whether the file is
open or not using file.is_open(). This if statement ensures that the file is open before we do any
read or write operations on it. We don't want to perform any operations on a closed file would we? If the code is
unable to open the file then it will simply move into the else block, priniting out "Unable to open the file" on the
console screen.
Moving inside the if statement block, we got:
file.seekg(0, ios::end);
int filePointerLocation = file.tellg();
Let's understand what seekg(0, ios::end) does. This
moves the file pointer from the start of the text file to the end of the file. The first parameter in seekg, 0 is
the offset and
ios::end is the reference point. The offset can either be a positive or negative value. Positive value means the
file
pointer moves from start start to finish i.e. forwards direction and negative value indicated that it should move
backwards i.e. towards the beginning of the file. The second parameter that is the reference point could either be
ios::beg, ios::cur or ios::end.
- ios::beg tells the code that the offset is relative to the beginning of the file.
- ios::cur tells the code that the offset is relative to the current location of the file
pointer.
- and ios::end tells the code that the offset is relative to the end of the file.
After that we are creating a int variable
filePointerLocation. Inside this variable, we are storing the file pointer current location using
file.tellg(). The tellg() method is used to get the current location of the file
pointer. Here, we are getting the current location of the file pointer and storing it in the
filePointerLocation varaible.
After finding the location of the file pointer we need to check whether
the location of the file pointer is 0 or not. If the file pointer is at 0 then this indicates that the file is
empty.
if (filePointerLocation == 0)
cout << "Nothing in this file!" << endl;
else
cout << "This file has content!" << endl;
In this If-Else block, we are simply checking if the
filePointerLocation is equal to 0 or not. If it is equal to 0, then that means the file is empty
and we print "The file is empty" on the console screen and if the file is not empty then it prints "The file is not
empty." on the console screen.
Lastly, we are closing the file using the close()
method.
When working with files in any programming language, always remember to close the files
becase files and file streams take up resources on the computer. Closing the files and disposing the streams
free up those
resources.
Writing the isFileEmpty Function
Now, let's write the main method. In the main method we are going to store the file path in a string varaible filePath and then we are gonig to pass this value to the isFileEmpty method.
int main() {
string filePath = "emptyTxtFile.txt";
isFileEmpty(filePath);
filePath = "non-emptyTxtFile.txt";
isFileEmpty(filePath);
return 0;
}
We are going to call the method two times. In the first call we are
going to pass it the path of an empty text file and in the second call we are going to pass it the path of text file
that have some context inside it.
Step 3: Complete Code & Code Demonstration: Compiling Code and Output
You can have a look at the complete code here:
Complete Code
#include <iostream>
#include <fstream>
using namespace std;
void isFileEmpty(string filePath) {
ifstream file(filePath);
if (file.is_open()) {
file.seekg(0, ios::end);
int fileSize = file.tellg();
if (fileSize == 0)
cout << "Nothing in this file!" << endl;
else
cout << "This file has content!" << endl;
file.close();
} else {
cout << "Unable to open the file." << endl;
}
}
int main() {
string filePath = "emptyTxtFile.txt";
isFileEmpty(filePath);
filePath = "non-emptyTxtFile.txt.txt";
isFileEmpty(filePath);
return 0;
}
Code Compilation & Output
For those who are using an online compiler or Visual Studio with the C++ Workload installed, you can simply run the code but for those who are using g++ compiler, you can compile the code running the g++ main.cpp command in the command line or the terminal.

This will generate a.exe file. To need to run the
a.exe file (which is an executable file) to get the output of our program. To run the executable file on
the terminal, run this command .\a.exe on the terminal. This will display the code's output on the terminal.

Congratulations! You have successfully written a program in C++ that
tells you if a file is empty or not.
Q: Do I need to use the fstream library? Are
there any atlernatives?
A: The fstream is one of the common and most widely use libraries in c++. This library is used to perform
read and write operation on files. To check if a file is empty or not. You are going to need to include this library in your code.
There are no appropriate atlernatives to this library.
Q: Does this only work for txt file?
A: No, this program can work for most file extensions such as a py file, or an executable etc.
Q: What is g++? Why do I need it?
A: G++ is an open-source c++ compiler and is a part of GNU Compiler Collection's C++ compilers. GNU is a set of
open source compilers for a lot of programming langauges. G++ isn't the only c++ compilers. There are several
other compilers that you can use such as Clang instead of G++.
Wrapping Up
Now, you know how to check whether a file is empty or not. File handling plays a crucial role when you need to store and access data permanently during the execution of a program. Checking whether a file is empty or not becomes important when you are working with files that might hold important information and you don't want to override such information. If the file is empty, you can simply exit the program or skip unnecessary lines of code which else may increase the execution time of the program. This can also be important when working with log files or when you are performing a clean up on old files or validating input values. Thanks for reading this guide. Happy Coding!
Reference