Solved

is_open() not in ifstream ?

Posted on 2008-10-15
8
1,846 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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

623 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