Checking for an Empty File in C++

How do i check if a file is empty in C?

Call ftell() does not tell you the size of the file. From the man page:

The ftell() function obtains the current value
of the file position indicator for the stream
pointed to by stream.

That is, it tell you your current position in the file...which will always be 0 for a newly opened file. You need to seek to the end of the file first (see fseek()).

check if file is empty or not in c

it does'nt work because you have to fopen it and fseek to the end first:

FILE *fptr;
if ( !( fptr = fopen( "saving.txt", "r" ))){
printf( "File could not be opened to retrieve your data from it.\n" );
}
else {
fseek(fptr, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(fptr);
if (len > 0) { //check if the file is empty or not.
rewind(fptr);
while ( !feof( fptr ) ){
fscanf( fptr, "%f\n", &p.burst_time );
AddProcess(&l,p.burst_time);
}
}
fclose( fptr );
}

How can I know text file is empty or not?(in C)

If the file was successfully open for read, as per fopen(filename, "r"), you can verify if it is empty before any read operation this way:

int is_empty_file(FILE *fp) {
int c = getc(fp);
if (c == EOF)
return 1;
ungetc(c, fp);
return 0;
}

ungetc() is guaranteed to work for at least one character. The above function will return 1 if the file is empty or if it cannot be read, due to an I/O error. You can tell which by testing ferr(fp) or feof(fp).

If the file is a stream associated to a device or a terminal, the test code will block until at least one byte can be read, or end of file is signaled.

If the file is a regular file, you could also use a system specific API to determine the file size, such as stat, lstat, fstat (on Posix systems).

Checking for an empty file in C++

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

What empty binary files contain in C

Function fopen returns a pointer to a file handle of type FILE, not a pointer to any content of the file or a pointer to an EOF-character. The pointer is NULL if the file could not be opened at all, but does not indicate whether the file is empty or not.

To check if a file is empty you either (1) need to make an attempt to read bytes and handle the various results, or (2) to use fseek and ftell to move the read pointer to the end and ask then for the position.

(1)

fp=fopen("clients.dat","rb");
char buffer;
size_t bytesRead = fread(&buffer, 1, 1, fp); // try to read one byte
if(bytesRead == 1) {
printf("file contains at least one byte\n");
} else { // error handling
if (feof(fp))
printf("Attemt to read though end of file has been reached. File is empty.\n");
else if (ferror(fp)) {
perror("Error reading file.");
}
}

(2)

fp=fopen("clients.dat","rb");
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
if (size==0) {
// file is empty.
}

I'd prefer the second variant.

File empty check


but the problem is if the first line is a '\n' and next line is EOF then ftell will return 2 but not 0

you do not want to know if a file is empty meaning its size is 0, but if the file contain something else that space, tab, newline etc, in that case the size is not enough. One way to do that can be :

#include <stdio.h>

int main(int argc, char ** argv)
{
FILE * fp;

if (argc != 2)
fprintf(stderr, "Usage %s <file>\n", *argv);
else if ((fp = fopen(argv[1], "r")) == NULL)
perror("cannot read file");
else {
char c;

switch (fscanf(fp, " %c", &c)) { /* note the space before % */
case EOF:
puts("empty or only spaces");
break;
case 1:
puts("non empty");
break;
default:
perror("cannot read file");
break;
}
fclose(fp);
}

return 0;
}

In fscanf(fp, " %c", &c) the space before % ask to bypass whitespaces (space, tab, newline ...)

Compilation and executions:

pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out /dev/null
empty or only spaces
pi@raspberrypi:/tmp $ echo > e
pi@raspberrypi:/tmp $ wc -c e
1 e
pi@raspberrypi:/tmp $ ./a.out e
empty or only spaces
pi@raspberrypi:/tmp $ echo " " > e
pi@raspberrypi:/tmp $ echo " " >> e
pi@raspberrypi:/tmp $ wc -c e
8 e
pi@raspberrypi:/tmp $ ./a.out e
empty or only spaces
pi@raspberrypi:/tmp $ echo "a" >> e
pi@raspberrypi:/tmp $ cat e


a
pi@raspberrypi:/tmp $ ./a.out e
non empty
pi@raspberrypi:/tmp $
pi@raspberrypi:/tmp $ chmod -r e
pi@raspberrypi:/tmp $ ./a.out e
cannot read file: Permission denied
pi@raspberrypi:/tmp $ ./a.out
Usage ./a.out <file>
pi@raspberrypi:/tmp $

How to detect an empty file in C++?

Use peek like following

if ( inputFile.peek() == std::ifstream::traits_type::eof() )
{
// Empty File

}


Related Topics



Leave a reply



Submit