Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

solved it ( was: linux - trouble with ifstream an FILE* (how do you/can you combine these somehow?)

167 views
Skip to first unread message

Marcel

unread,
Jun 8, 2003, 7:40:12 PM6/8/03
to
Jim Fischer wrote:
>> Marcel wrote:
>>
>> Is there a way in which I can open a file using a C 'open'
>> (or fopen or popen) and then 'convert' that somehow into a C++
>> ifstream or istream?
>
>
> This is not supported by the ISO/ANSI C++ iostreams class library. More
> specifically, the ISO/ANSI C++ iostream class library does not have
> built-in support for the C library's 'FILE' data type.

Yeah, I figured that...seemed liked the most obvious place to ask :o
(Where can you go for answers when your question is a combination of C++
and C? )

> I think you'll need to use a Linux-specific solution for this. Since
> OS-specific HOWTOs are considered off-topic in the C/C++ language
> groups, your best bet would be to ask for help in the Linux programming
> newsgroups, e.g.,
>
> comp.os.linux.development.apps
>
> [OT Hint: man popen(3), but you didn't hear that from me... ;-)]

Man pages are not as helpful as they could be...if only they included
some examples in those things :o And the man pages are C exclusive, you
certainly wouldn't find any info on how to pipe C streams into C++
streams there ;^) I a nosy through the fstream.h header file...that works.

I found the solution btw:

ifstream f;

f.attach( fd );

of just:

ifstream f( fd );

(where fd is obtained from: int fd = fileno( popen(...) );

And yes, that is linux/unix specific.
My original code was fine, but for some reason my compiler wouldn't have
any of it.
(I'm using gcc 3.2 btw) When I compiled with an older version of gcc
everything compiled and worked fine...make sense of that. Humbug.

This is not the first time I've run into this problem...here I was
thinking "I'm using a nice new version of gcc, won't have any
compatibility problems with my code and all"...well, I'm getting more
and more convinced that the opposite is true :(

Now I'll just need to figure out the reverse (but I reckon I can find
that with google ;^)

Thank anyway :)

Marcel
---
Please remove the capital U's from my address when replying by mail :^)

0 new messages