3 Replies - 270 Views - Last Post: 10 December 2012 - 07:32 PM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Exit method safely

Posted 10 December 2012 - 02:15 AM

hi
I have wrote this method to read files the question is about this code here

if( fileName.fail() )
{
        cerr << "Cannot open file: " << endl;
	system( "PAUSE" );
	return( 1 );
}



I know my method doesn't return any think because it is void so how would I exit this method safely if the file cannot be found?
Any help would be appreciated

void ReadFile::getInfo()
{
	string info;
	fileName.open( "Test.txt" );

	if( fileName.fail() )
	{
		cerr << "Cannot open file: " << endl;
		system( "PAUSE" );
		return( 1 );
	}

	fileName >> info;
	cout << info << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Exit method safely

#2 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Re: Exit method safely

Posted 10 December 2012 - 02:21 AM

Hi
would you use some think like

exit( EXIT_FAILURE );


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5942
  • View blog
  • Posts: 12,870
  • Joined: 16-October 07

Re: Exit method safely

Posted 10 December 2012 - 05:44 AM

Perhaps:
void ReadFile::getInfo() {
	string info;
	fileName.open( "Test.txt" );

	if( fileName.fail() ) {
		cerr << "Cannot open file: " << endl;
		system( "PAUSE" );
	} else {
		fileName >> info;
		cout << info << endl;
	}
}



Though, honestly, one method shouldn't be getting that much action. And if this is part of class...

Why is fileName defined at class level but the actual filename isn't? Why the hell is an object of presumably fstream called fileName at all? When do you close it?

Perhaps:
const char *ReadFile::getFileName() const { return "Test.txt"; }

bool ReadFile::openFile(fstream &fs) {
	fs.open( getFileName() );
	if( fs.fail() ) {
		cerr << "Cannot open file: " << endl;
		system( "PAUSE" ); // nasty, be we'll level it
		return false;
	}
	return true;
}

bool ReadFile::getInfo(string &info) {
	fstream fs;
	if (openFile(fs)) {
		fs >> info;
		fs.close(); // never forget this part
		return true;
	}
	return false;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Exit method safely

Posted 10 December 2012 - 07:32 PM

Quote

I know my method doesn't return any think because it is void so how would I exit this method safely if the file cannot be found?

You should probably return some type of error code so that main can handle the error accordingly. If your not going to return anything, then you can resort to throwing an exception.


#include <iostream>
#include <exception>
#include <fstream>

struct bad_file : public std::exception {};

void getInfo(const char *filename, std::string &info) {
   std::ifstream file(filename);
   if(!file.is_open()) 
      throw bad_file();
   file>>info;
   file.close();
}
int main()
{
   std::string info;
   try {
      getInfo("test.txt", info);
   } catch(bad_file &) {
      std::cerr<<"Could not open file"<<std::endl;
   }

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1