Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

is_open() not in ifstream ?

Posted on 2008-10-15
8
Medium Priority
?
1,900 Views
Last Modified: 2013-11-17
I am compiling on multiple Unix platforms.

The statements:

  ifstream file("o", ios::binary | ios::ate);
       
  if(file.is_open()) { ...

generate a compiler error: "is_open is not a member of "class ifstream".

How can I fix this for the AIX platform ?

0
Comment
Question by:pillmill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22726202
That's probably because you need to specify the std namespace :
#include <fstream>         // <--- you need this header ... note that there's NO .h at the end
 
std::ifstream file("o", ios::in | ios::binary | ios::ate);
 
if (file.is_open()) {
  // ...
}

Open in new window

0
 
LVL 1

Expert Comment

by:siddhant3s
ID: 22728723
Apart from what infinity08 replied,
I suppose the main problem lies when you dont close your files after using your code:

....
....
...
 
file.close();
return 0;
}//end of int main

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22728777
>> I suppose the main problem lies when you dont close your files after using your code:

That shouldn't cause this kind of compiler error though ;)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22730863
>>>>  #include <fstream>  // <--- you need this header ... note that there's NO .h

I think it is the old fstream.h included where ifstream class doesn't have is_open member function. Otherwise the error message would have called of 'basic_ifstream' class what is the original name of std::ifstream
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22730893
That's why I added that comment ;)
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 22730975
>>>> if(file.is_open()) {
In case I am wrong and you were using std::basic_ifstream but nevertheless don't have  is_open member function you simply could do



  std::ifstream file("o", ios::binary | ios::ate);
       
  if(file) { ...

In case of an open error the ifstream sets the fail-bit what could be checked by

   if (file)

Note, if using old ifstream the latter won't work. You then have to check for the fail-bit, e. g. by

  if (!file.good())    


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22731065
>>>> That's probably because you need to specify the std namespace
>>>> That's why I added that comment ;)

Yes, that's why I refered to ;-)

... but the first statement could be misinterpreted


0
 
LVL 1

Expert Comment

by:siddhant3s
ID: 22742973
My apologies.
Though I dont usually do this, I superficially read the question. I gave the answer without considering that there is a compilation error.
In that case, I think you should follow infinity.
PS; "Its a good practice to close your files though."

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question