6
Contributors
11
Replies
27
Views
7 Years
Discussion Span
Last Post by sanof3322
Featured Replies
  • Related: [url]http://c-faq.com/osdep/filesize.html[/url] Read More

  • 1
    Narue 5,707   7 Years Ago

    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: [code] #include <fstream> #include <iostream> int main() { const char *filename = "test.txt"; std::ifstream in ( filename ); if ( !in ) std::cerr<< … Read More

  • Assuming same things as Narue, you can also just peek the character without reading it, just in case the file is not empty. [code] #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 … Read More

0

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.

0

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";
    }
}
1

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";
  }
}
1

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.

0

>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.

Edited by Narue: n/a

0

>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...

0

>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.

1

If file successfully open...

Read the first value

if (myFile.fail())               //if reading fails, file is empty
      cout << "empty file";
Votes + Comments
but it is not working
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.