ifstream::is_open() not found on Solaris C++ compiler

A

ambar.shome

Hi,
We are working on Solaris SunOS 5.8. We are using ifstream of iostream
namespace. The snippet is given below:

#include <iostream.h>
#include <fstream.h>

boolean readFile()
{
char readStr[256];

ifstream infile;
infile.open("123768_20010706_2001.csv");

if (infile.is_open())
{
while (infile.good())
{
infile.getline(readStr,256,'\n');
cout << "readStr:::="<<readStr<<endl;

}
infile.close();

}
else
{
cout << "Error opening file";
}
return true;

}
but on compilation the compiler is giving an error message that

could not find the function is_open()
is it not available on Solaris compiler??

Can anyone enlighten me on that??
 
L

Lionel B

Hi,
We are working on Solaris SunOS 5.8. We are using ifstream of iostream
namespace. The snippet is given below:

#include <iostream.h>
#include <fstream.h>

boolean readFile()
{
char readStr[256];

ifstream infile;
infile.open("123768_20010706_2001.csv");

if (infile.is_open())
{
while (infile.good())
{
infile.getline(readStr,256,'\n');
cout << "readStr:::="<<readStr<<endl;

}
infile.close();

}
else
{
cout << "Error opening file";
}
return true;

}
but on compilation the compiler is giving an error message that

could not find the function is_open()
is it not available on Solaris compiler??

Can anyone enlighten me on that??

I'm not sure if is_open() is standard or not. gcc seems to have it,
can't find it in Stroustrup. In any case, it seems somewhat redun-
dant, since if open() fails it sets the stream's failbit, which will
be tested by good() anyway...

(BTW what is "boolean"?)

HTH,
 
P

P.J. Plauger

(e-mail address removed) wrote:
We are working on Solaris SunOS 5.8. We are using ifstream of iostream
namespace. The snippet is given below:

#include <iostream.h>
#include <fstream.h>

boolean readFile()
{
char readStr[256];

ifstream infile;
infile.open("123768_20010706_2001.csv");

if (infile.is_open())
{
while (infile.good())
{
infile.getline(readStr,256,'\n');
cout << "readStr:::="<<readStr<<endl;

}
infile.close();

}
else
{
cout << "Error opening file";
}
return true;

}
but on compilation the compiler is giving an error message that

could not find the function is_open()
is it not available on Solaris compiler??

Can anyone enlighten me on that??

I'm not sure if is_open() is standard or not. gcc seems to have it,
can't find it in Stroustrup. In any case, it seems somewhat redun-
dant, since if open() fails it sets the stream's failbit, which will
be tested by good() anyway...

is_open is required by the C++ Standard.
(BTW what is "boolean"?)

It's a value of type bool, which stores either true or false.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
B

bjarne

is_open() is in the standard.

It is also in TC++PL, but the index let me (us?) down. Thanks for
pointing it out. It's mentioned as a member of basic_ostream on pg 638
and of basic_streambuf on page 648.

I suspect that the reason I give it so short shift is that I tend to
use
if(os) ...
rather than
if(os.is_open())

A boolean is a value used for logocal computations. In C++, it means a
value of type bool

-- Bjarne Stroustrup; http://www.research.att.com/~bs
 
B

Bob Hairgrove

Hi,
We are working on Solaris SunOS 5.8. We are using ifstream of iostream
namespace. The snippet is given below:

#include <iostream.h>
#include <fstream.h>

The *.h headers are deprecated. But I have never worked on Solaris, so
maybe this is an old version of their cc? Nowadays, one uses:

#include <iostream>
#include <fstream>

Try it and see if it clears up the problem.

[BTW, is <istream> automatically included by <iostream>? I seem to
boolean readFile()
{
char readStr[256];

ifstream infile;
infile.open("123768_20010706_2001.csv");

if (infile.is_open())
{
while (infile.good())
{
infile.getline(readStr,256,'\n');
cout << "readStr:::="<<readStr<<endl;

}
infile.close();

}
else
{
cout << "Error opening file";
}
return true;

}
but on compilation the compiler is giving an error message that

could not find the function is_open()
is it not available on Solaris compiler??

Can anyone enlighten me on that??
 
O

Old Wolf

bjarne said:
is_open() is in the standard.

I suspect that the reason I give it so short shift is that I tend to
use
if(os) ...
rather than
if(os.is_open())

I fell into a trap using this syntax, recently:

ifstream f;
if (foo())
f.open("pathname");

if (f)
{
// read stuff from file....
}
else
cout << "File is not open.\n";

The problem was that if you never try to open the fstream,
it never gets any fail bits set, so (f) evaluates to true,
wherewas f.is_open() would have been false.
 
L

Lionel B

P.J. Plauger said:
is_open is required by the C++ Standard.
Yup.


It's a value of type bool, which stores either true or false.

I know what the word "boolean" means; however AFAIK it is not a standard
type and doesn't appear to have been defined in the OP's code - why not
just use "bool"?
 
P

P.J. Plauger

I know what the word "boolean" means; however AFAIK it is not a standard
type and doesn't appear to have been defined in the OP's code - why not
just use "bool"?

Because of a very long tradition of using the term Boolean in math?
George Boole (http://www.kerryr.net/pioneers/boole.htm) developed
modern two-value logic early in the 19th century. We use the term
Boolean in his honor to describe his math. bool is a recent
degenerate variant used in some programming languages; and boolean
is an inevitable back formation.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top