Solved

is_open() not in ifstream ?

Posted on 2008-10-15
8
1,730 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

823 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