Is there any if statement to check if a file is empty or not?

Recommended Answers

All 11 Replies

seek to end of file, then get the file pointer. If 0, its an empty file. How to do that depends on whether you want to use fstream or FILE.

Wel i used fstream...

call seekp()

ifstream in("myfile.txt");
if(in.is_open())
{
    in.seekp(0,ios::end);
    size_t size = in.tellg();
    if( size == 0)
    {
         cout << "File is empty\n";
    }
}

Assuming by "empty" you mean if you try to read any characters, the input will fail immediately at end-of-file, then testing for an empty file is trivial:

#include <fstream>
#include <iostream>

int main()
{
  const char *filename = "test.txt";
  std::ifstream in ( filename );

  if ( !in )
    std::cerr<< filename <<" failed to open\n";
  else {
    // Read a character, test for end-of-file
    bool empty = ( in.get(), in.eof() );

    std::cout<< filename <<" is "
      << ( empty ? "" : "not " )
      << "empty\n";
  }
}

Assuming same things as Narue, you can also just peek the character
without reading it, just in case the file is not empty.

#include<iostream>
#include<fstream>

using namespace std;
 

int main()
{
	ifstream read("test.txt");

	if(!read) return 0;

	bool isEmpty = read.peek() == EOF;
	
	cout << boolalpha << "test is empty = " << isEmpty<<endl;

	return 0;
}

That way, if you read the file, and its not empty, you don't have to set
the cursor back to the beginning.

Is there any way to convert Commmand line interface to GUI in C++?

>Is there any way to convert Commmand line interface to GUI in C++?
Yes. One way is to separate your core functionality from the interface, then hook into that core functionality using a command line interface and a GUI interface. Now you have an application that can run both in a GUI and on the command line. As you might imagine, that design takes some forethought and is far from an automatic conversion with an existing program.

>Is there any way to convert Commmand line interface to GUI in C++?
Yes. One way is to separate your core functionality from the interface, then hook into that core functionality using a command line interface and a GUI interface. Now you have an application that can run both in a GUI and on the command line. As you might imagine, that design takes some forethought and is far from an automatic conversion with an existing program.

Well can we go through one of my program step by step together to design it into gui because i only know how to do programs in command line...

>Well can we go through one of my program step by step together to design it into gui
No. Firstly, that's a non-trivial exercise for a forum, and second, I'm not qualified to teach you how to write GUIs. You might try Daniweb's IRC chat room for an overview, or just learn the basics of GUI design and work your way up.

>i only know how to do programs in command line...
I make an excellent living writing non-GUI code. Just in case you were under the impression that such knowledge is required.

If file successfully open...

Read the first value

if (myFile.fail())               //if reading fails, file is empty
      cout << "empty file";
commented: but it is not working +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.