Solved

is_open() not in ifstream ?

Posted on 2008-10-15
8
1,672 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now