<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged is-empty</title>
<link>https://coderlegion.com/tag/is-empty</link>
<description>Powered by Question2Answer</description>
<item>
<title>How to Check If File is Empty in C++</title>
<link>https://coderlegion.com/7692/how-to-check-if-file-is-empty-in-c</link>
<description>&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;To &quot;check if file is empty in C++&quot;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use the &lt;i&gt;&lt;strong&gt;fstream&lt;/strong&gt;&lt;/i&gt; for working with files in cpp.&lt;/li&gt;
&lt;li&gt;Write a function &lt;i&gt;&lt;strong&gt;isFileEmpty&lt;/strong&gt;&lt;/i&gt; with a return type of void and it is going to take only one string parameter.&lt;/li&gt;
&lt;li&gt;Finally, inside the main method, pass the file path to the &lt;i&gt;&lt;strong&gt;isFileEmpty&lt;/strong&gt;&lt;/i&gt; function and it's going to tell us whether the file is empty or not.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Check if a File in Empty in C++&lt;/h2&gt;
&lt;h3&gt;Step 1: Development Environment: What Are You Going to Need?&lt;/h3&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;You can use an IDE installed locally on your computer like
    &lt;a rel=&quot;nofollow&quot; href=&quot;https://code.visualstudio.com/&quot;&gt;Visual Studio Code&lt;/a&gt; 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 &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.onlinegdb.com/online_c++_compiler&quot;&gt;OnlineGDB&lt;/a&gt; or &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.programiz.com/cpp-programming/online-compiler&quot;&gt;Programiz&lt;/a&gt;. If you are using a locally
    installed IDE or a Code Editior, make sure it has the &lt;i&gt;&lt;strong&gt;fstream&lt;/strong&gt;&lt;/i&gt; library installed.&lt;/p&gt;
&lt;h3&gt;Step 2: Check Empty File: How to Check For an Empty File in C++&lt;/h3&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Let's start by including all the necessay libraries. Make sure to include this part at the top of the code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;;
#include &amp;lt;fstream&amp;gt;;
using namespace std;
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;The &lt;i&gt;&lt;strong&gt;iostream&lt;/strong&gt;&lt;/i&gt; library is used for the standard input &amp;amp;
    output operations in C++ and the &lt;a rel=&quot;nofollow&quot; href=&quot;https://cplusplus.com/reference/fstream/fstream/&quot; title=&quot;rand&quot;&gt;fstream&lt;/a&gt; library is used for working with files and directories.
    Lastly, the &lt;strong&gt;using namespace std;&lt;/strong&gt; part is used for allowing you to use standard C++ library
    components without needing the std:: prefix.&lt;/p&gt;
&lt;div class=&quot;div-blue&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt; 
&lt;span class=&quot;alert-body&quot;&gt;If you are not going to use &lt;strong&gt;using namespace std;&lt;/strong&gt; in your code, you will have to use the std:: prefex before using standard C++ library components.&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Writing the isFileEmpty Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Let's start working on the &lt;strong&gt;isFileEmpty&lt;/strong&gt; function with a return type of void. This function should only take one string parameter.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void isFileEmpty(string filePath) {
    ifstream file(filePath);
    
    if (file.is_open()) {
        file.seekg(0, ios::end);
        int filePointerLocation = file.tellg();
    
        if (filePointerLocation == 0)
            cout &amp;lt;&amp;lt; &quot;Nothing in this file!&quot; &amp;lt;&amp;lt; endl;
        else 
            cout &amp;lt;&amp;lt; &quot;This file has content!&quot; &amp;lt;&amp;lt; endl;
    
        file.close();
    } else {
           cout &amp;lt;&amp;lt; &quot;Unable to open the file.&quot; &amp;lt;&amp;lt; endl;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;In this function, we are taking a single string paramater &lt;i&gt;&lt;strong&gt;filepath&lt;/strong&gt;&lt;/i&gt;. This variable is going to store the path to our text file that we are going to check.&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Moving inside the function, first we got this line of code: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ifstream file(filePath);
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;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.&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;After that we got an if statement that is checking whether the file is
    open or not using &lt;strong&gt;file.is_open()&lt;/strong&gt;. 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 &quot;Unable to open the file&quot; on the
    console screen.&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Moving inside the if statement block, we got: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;file.seekg(0, ios::end);
int filePointerLocation = file.tellg();
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Let's understand what &lt;strong&gt;seekg(0, ios::end)&lt;/strong&gt; 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
    &lt;strong&gt;ios::beg&lt;/strong&gt;, &lt;strong&gt;ios::cur&lt;/strong&gt; or &lt;strong&gt;ios::end&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;ios::beg&lt;/strong&gt; tells the code that the offset is relative to the beginning of the file.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;ios::cur&lt;/strong&gt; tells the code that the offset is relative to the current location of the file
        pointer.&lt;/li&gt;
    &lt;li&gt;and &lt;strong&gt;ios::end&lt;/strong&gt; tells the code that the offset is relative to the end of the file.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;After that we are creating a int variable
    &lt;i&gt;&lt;strong&gt;filePointerLocation&lt;/strong&gt;&lt;/i&gt;. Inside this variable, we are storing the file pointer current location using
    &lt;strong&gt;file.tellg()&lt;/strong&gt;. The &lt;strong&gt;tellg()&lt;/strong&gt; 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
    &lt;i&gt;&lt;strong&gt;filePointerLocation&lt;/strong&gt;&lt;/i&gt; varaible.&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;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.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (filePointerLocation == 0)
            cout &amp;lt;&amp;lt; &quot;Nothing in this file!&quot; &amp;lt;&amp;lt; endl;
        else 
            cout &amp;lt;&amp;lt; &quot;This file has content!&quot; &amp;lt;&amp;lt; endl;
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;In this If-Else block, we are simply checking if the
    &lt;i&gt;&lt;strong&gt;filePointerLocation&lt;/strong&gt;&lt;/i&gt; is equal to 0 or not. If it is equal to 0, then that means the file is empty
    and we print &quot;The file is empty&quot; on the console screen and if the file is not empty then it prints &quot;The file is not
    empty.&quot; on the console screen.&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Lastly, we are closing the file using the &lt;strong&gt;close()&lt;/strong&gt;
    method.&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
    &lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
    &lt;span class=&quot;alert-body&quot;&gt;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.&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Writing the isFileEmpty Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Now, let's write the main method. In the main method we are going to store the file path in a string varaible &lt;i&gt;&lt;strong&gt;filePath&lt;/strong&gt;&lt;/i&gt; and then we are gonig to pass this value to the &lt;i&gt;&lt;strong&gt;isFileEmpty&lt;/strong&gt;&lt;/i&gt; method.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int main() {
        string filePath = &quot;emptyTxtFile.txt&quot;;
        isFileEmpty(filePath);
    
        filePath = &quot;non-emptyTxtFile.txt&quot;;
        isFileEmpty(filePath);
        
        return 0;
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;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.&lt;/p&gt;
&lt;h3&gt;Step 3: Complete Code &amp;amp; Code Demonstration: Compiling Code and Output&lt;/h3&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;You can have a look at the complete code here: &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Complete Code&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;fstream&amp;gt;
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 &amp;lt;&amp;lt; &quot;Nothing in this file!&quot; &amp;lt;&amp;lt; endl;
        else 
            cout &amp;lt;&amp;lt; &quot;This file has content!&quot; &amp;lt;&amp;lt; endl;
        
        file.close();
    } else {
            cout &amp;lt;&amp;lt; &quot;Unable to open the file.&quot; &amp;lt;&amp;lt; endl;
    }
}
        
int main() {
string filePath = &quot;emptyTxtFile.txt&quot;;
    isFileEmpty(filePath);
        
    filePath = &quot;non-emptyTxtFile.txt.txt&quot;;
    isFileEmpty(filePath);
            
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Code Compilation &amp;amp; Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;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 &lt;strong&gt;g++ main.cpp&lt;/strong&gt; command in the command line or the terminal.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=12403707227507160495&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This will generate &lt;strong&gt;a.exe&lt;/strong&gt; file. To need to run the
    &lt;strong&gt;a.exe&lt;/strong&gt; file (which is an executable file) to get the output of our program. To run the executable file on
    the terminal, run this command &lt;strong&gt;.\a.exe&lt;/strong&gt; on the terminal. This will display the code's output on the terminal.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=10919166762266017553&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Congratulations! You have successfully written a program in C++ that
    tells you if a file is empty or not.&lt;/p&gt;
&lt;div class=&quot;div-faq&quot; style=&quot;margin-bottom: 15px;&quot;&gt;
    &lt;span class=&quot;faq-header&quot; style=&quot;font-weight: bold; color: dark-gray;&quot;&gt;FAQs&lt;/span&gt;
    &lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px; margin-bottom: 20px;&quot;&gt;Q: Do I need to use the fstream library? Are
        there any atlernatives?
        &lt;br&gt;
        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.
    &lt;/span&gt;
    &lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px; margin-bottom: 20px;&quot;&gt;Q: Does this only work for txt file?
        &lt;br&gt;
        A: No, this program can work for most file extensions such as a py file, or an executable etc.&lt;/span&gt;
    &lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px;&quot;&gt;Q: What is g++? Why do I need it?
        &lt;br&gt;
        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++.&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;Wrapping Up&lt;/h2&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;Now, you know how to check whether a file is empty or not. File handling plays a crucial role when you need to &lt;i&gt;&lt;strong&gt;store and access data permanently&lt;/strong&gt;&lt;/i&gt; 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 &lt;i&gt;&lt;strong&gt;don't want to override&lt;/strong&gt;&lt;/i&gt; 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 &lt;i&gt;&lt;strong&gt;performing a clean up&lt;/strong&gt;&lt;/i&gt; on old files or &lt;i&gt;&lt;strong&gt;validating input values&lt;/strong&gt;&lt;/i&gt;. Thanks for reading this guide. Happy Coding!&lt;/p&gt;
&lt;hr&gt; 
&lt;h2&gt; Reference&lt;/h2&gt; 
&lt;ul&gt;
 &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://cplusplus.com/forum/unices/13699/&quot; title=&quot;rand&quot;&gt; Check For Empty File&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://cplusplus.com/doc/tutorial/files/&quot; title=&quot;rand&quot;&gt;File Handling in C++&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/7692/how-to-check-if-file-is-empty-in-c</guid>
<pubDate>Mon, 24 Nov 2025 00:16:00 +0000</pubDate>
</item>
</channel>
</rss>