Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

is_open() not in ifstream ?

Posted on 2008-10-15
8
Medium Priority
?
1,985 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

916 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