Crystal
Crystal

Reputation: 29458

Checking for an empty file in C++

Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks.

Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'.

My function's parameter is

myFunction(ifstream &inFile)

Upvotes: 53

Views: 161998

Answers (10)

Antonio
Antonio

Reputation: 20266

If your use case offer the possibility to check for emptiness before opening the file,
C++17 provides you is_empty

#include <filesystem>

if (!std::filesystem::is_empty("path.txt")) {
     ///Open and use the file
}

Upvotes: 1

Aylian Craspa
Aylian Craspa

Reputation: 466

when the file is empty the tellg will give you value 0 if its empty so focus on that and it is the simplest way to find an empty file, if you just create the file it will give you -1.

outfile.seekg(0,ios::end);
if(file.tellg()<1){
  //empty
}else{
  file.clear(); // clear all flags(eof)
  file.seekg(0,ios::beg);//reset to front
  //not empty
}

Upvotes: 1

Mansoor
Mansoor

Reputation: 2438

C++17 solution:

#include <filesystem>

const auto filepath = <path to file> (as a std::string or std::filesystem::path)

auto isEmpty = (std::filesystem::file_size(filepath) == 0);

Assumes you have the filepath location stored, I don't think you can extract a filepath from an std::ifstream object.

Upvotes: 4

Vytaute
Vytaute

Reputation: 9

use this: data.peek() != '\0'

I've been searching for an hour until finaly this helped!

Upvotes: 0

amna
amna

Reputation: 1

if (nfile.eof()) // Prompt data from the Priming read:
    nfile >> CODE >> QTY >> PRICE;
else
{
    /*used to check that the file is not empty*/
    ofile << "empty file!!" << endl;
    return 1;
}

Upvotes: -1

CroCo
CroCo

Reputation: 5741

How about (not elegant way though )

int main( int argc, char* argv[] )
{
    std::ifstream file;
    file.open("example.txt");

    bool isEmpty(true);
    std::string line;

    while( file >> line ) 
        isEmpty = false;

        std::cout << isEmpty << std::endl;
}

Upvotes: 0

user4471014
user4471014

Reputation: 9

char ch;
FILE *f = fopen("file.txt", "r");

if(fscanf(f,"%c",&ch)==EOF)
{
    printf("File is Empty");
}
fclose(f);

Upvotes: 0

GManNickG
GManNickG

Reputation: 503825

Perhaps something akin to:

bool is_empty(std::ifstream& pFile)
{
    return pFile.peek() == std::ifstream::traits_type::eof();
}

Short and sweet.


With concerns to your error, the other answers use C-style file access, where you get a FILE* with specific functions.

Contrarily, you and I are working with C++ streams, and as such cannot use those functions. The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof(). Ergo, we just peek() at the stream and see if it's eof(), since an empty file has nothing to peek at.

Note, this also returns true if the file never opened in the first place, which should work in your case. If you don't want that:

std::ifstream file("filename");

if (!file)
{
    // file is not open
}

if (is_empty(file))
{
    // file is empty
}

// file is open and not empty

Upvotes: 92

pajton
pajton

Reputation: 16226

Ok, so this piece of code should work for you. I changed the names to match your parameter.

inFile.seekg(0, ios::end);  
if (inFile.tellg() == 0) {    
  // ...do something with empty file...  
}

Upvotes: 9

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

Seek to the end of the file and check the position:

 fseek(fileDescriptor, 0, SEEK_END);
 if (ftell(fileDescriptor) == 0) {
     // file is empty...
 } else {
     // file is not empty, go back to the beginning:
     fseek(fileDescriptor, 0, SEEK_SET);
 }

If you don't have the file open already, just use the fstat function and check the file size directly.

Upvotes: 5

Related Questions