Connecting Tech Pros Worldwide Forums | Help | Site Map

how to check success of open function for ifstream

Member
 
Join Date: May 2007
Posts: 65
#1: Jul 13 '07
how can i check the success of open function for ifstream class?
see the code below:
Expand|Select|Wrap|Line Numbers
  1. ifstream fin;
  2. fin.open("myfile.dat");

devikacs's Avatar
Member
 
Join Date: Jun 2007
Location: India
Posts: 96
#2: Jul 13 '07

re: how to check success of open function for ifstream


Quote:

Originally Posted by stmfc

how can i check the success of open function for ifstream class?
see the code below:

Expand|Select|Wrap|Line Numbers
  1. ifstream fin;
  2. fin.open("myfile.dat");

public member function is_open would check if the file is open.
Or you can check if fin is null or not
archonmagnus's Avatar
Member
 
Join Date: Jun 2007
Location: The Interweb
Posts: 115
#3: Jul 13 '07

re: how to check success of open function for ifstream


Quote:

Originally Posted by stmfc

how can i check the success of open function for ifstream class?
see the code below:

Expand|Select|Wrap|Line Numbers
  1. ifstream fin;
  2. fin.open("myfile.dat");

Use the good() or fail() members of fstream. For example, in your case:

Expand|Select|Wrap|Line Numbers
  1. ifstream fin;
  2. fin.open("myfile.dat");
  3.  
  4. if (fin.good())
  5.     cout<<"Open Success!"<<endl;
  6. else
  7.     cout<<"Open Failure!"<<endl;
  8.  
Reply