Namespace clash for istream object :-(

V

vivek.astvansh

Guys,

I have the following piece of code.

int myClass::myfn()
{
std::ifstream myFile;
myFile.open("/home/astvansh/devmap");
std::string line;
getline(myFile, line);
....
}

"devmap" is a text file. getline( ) returns an empty string on HP-UX,
but reads the text file successfully on Solaris and HP-UX. This code is
compiled using a proprietary STL library.

I wrote this program separately as test.cpp, built it using
"/opt/aCC/bin/aCC test.cpp", and it worked well on HP-UX. Hence, the
only difference is in the build environment.

I suspect the problem is because of _STL namespace. The object myfile
in my code is
of type _STL::basic_istream.

May someone please help me.

Thanks very much.
 
J

Jonathan Mcdougall

Guys,

I have the following piece of code.

int myClass::myfn()
{
std::ifstream myFile;
myFile.open("/home/astvansh/devmap");
std::string line;
getline(myFile, line);
...
}

"devmap" is a text file. getline( ) returns an empty string on HP-UX,
but reads the text file successfully on Solaris and HP-UX. This code is
compiled using a proprietary STL library.

I wrote this program separately as test.cpp, built it using
"/opt/aCC/bin/aCC test.cpp", and it worked well on HP-UX. Hence, the
only difference is in the build environment.

I suspect the problem is because of _STL namespace. The object myfile
in my code is
of type _STL::basic_istream.

Are you sure the file is accessible? If iostreams were broken on my
compiler, I'd be afraid of my harddisk getting formatted next time I
compile something.

You could try posting in a newsgroup supporting your platform/compiler
to get more specific help.


Jonathan
 
V

vivek.astvansh

Jonathan, thanks. I modified the code to:

int myClass::myfn()
{
std::ifstream myFile;
myFile.open("/home/astvansh/devmap");
if(!myFile)
{
std::cout << "Could not open file." << std::endl;
exit(-1);
}
std::string line;
getline(myFile, line);
if(line.empty())
{
std::cout << "Empty file." << std::endl;
}
....

}

The output is "Empty file." :-(

Thanks for the suggestion, Jonathan. I shall post the Q on HP-UX
forums, too.
 
J

Jonathan Mcdougall

Jonathan, thanks. I modified the code to:

Please quote the message you are answering to.
int myClass::myfn()
{
std::ifstream myFile;
myFile.open("/home/astvansh/devmap");
if(!myFile)
{
std::cout << "Could not open file." << std::endl;
exit(-1);
}
std::string line;
getline(myFile, line);

Try to qualify std::getline(), ADL may not be used if there's another
getline() somewhere.
if(line.empty())
{
std::cout << "Empty file." << std::endl;
}
...

}

The output is "Empty file." :-(

Is it empty? You know, try the easy stuff first, you may be suprised.
Did you try to use fopen() to see if the problem is with C++ iostreams
or if it's more general?


Jonathan
 
V

vivek.astvansh

Thanks, Jonathan. Yes, I can read the file using fopen-fscanf. But the
iostream does not work :-(
 
S

Stuart Redmann

Yeah, thanks Jonathan.

I promise I'll never forget to quote the problem I'm answering to.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top