|
Home > Archive > Unix Programming > February 2004 > C++ streams and FILE* interoperability
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
C++ streams and FILE* interoperability
|
|
|
| I think streams are nice, but what do you do when you have to write to
or, even worse, read from a FILE*, for example a UNIX stream?
C++ streams can not be created from FILE*'s or have them attached.
Today, a C++ programmer has to decide beforehand whether he wants to
use C or C++ I/O. Suppose you choose C++ streams and write all of the
"operator>>" code for your objects. If you later decide to read the
same objects from a UNIX pipe, you'll have to rewrite and re-debug
much of that code.
Since C++ was aiming for seamless interoperability with C, wouldn't it
have been easier to make its file stream a shallow wrapper around
FILE*? It already has the necessary buffering functionality. Or at
least specialize the character streams to be such bufferless wrappers
around FILE* ?
On a more positive note, I haven't used them yet, but it looks like
STLport provides such shallow stream wrappers as an option.
| |
| joe@invalid.address 2004-02-16, 7:34 am |
| spam_bait101@yahoo.com (sb) writes:
> I think streams are nice, but what do you do when you have to write
> to or, even worse, read from a FILE*, for example a UNIX stream?
>
> C++ streams can not be created from FILE*'s or have them attached.
This isn't true. You can derive a streambuf class that will take a
FILE* or file descriptor as a constructor parameter, and use that to
get/send data. Josuttis gives an example of the latter in his book.
Neither FILE* or file descriptor is portable though, which is why
they're not supported in the C++ standard.
> Today, a C++ programmer has to decide beforehand whether he wants to
> use C or C++ I/O. Suppose you choose C++ streams and write all of
> the "operator>>" code for your objects. If you later decide to read
> the same objects from a UNIX pipe, you'll have to rewrite and
> re-debug much of that code.
Not if you do it right. It does take a pretty good understanding of
how the classes work though.
> Since C++ was aiming for seamless interoperability with C, wouldn't
> it have been easier to make its file stream a shallow wrapper around
> FILE*? It already has the necessary buffering functionality. Or at
> least specialize the character streams to be such bufferless
> wrappers around FILE* ?
I wouldn't argue with that. However, if you're really interested in
getting expert opinions about this (as opposed to random net.bozos
like me) ask this is comp.lang.c++.moderated.
Joe
--
"I didn't really say everything I said."
- Yogi Berra
| |
| Barry Margolin 2004-02-16, 9:34 am |
| In article <m3d68e3f99.fsf@invalid.address>, joe@invalid.address wrote:
> Neither FILE* or file descriptor is portable though, which is why
> they're not supported in the C++ standard.
I understand that file descriptors wouldn't be portable, but why not
FILE*? It's part of the C standard, and C++ incorporates most of C by
reference.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Erik Max Francis 2004-02-16, 10:34 am |
| Barry Margolin wrote:
> I understand that file descriptors wouldn't be portable, but why not
> FILE*? It's part of the C standard, and C++ incorporates most of C by
> reference.
I think he meant wrapping a streambuf around a FILE * wouldn't be
portable, not that FILE * themselves aren't portable.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Seriousness is the only refuge of the shallow.
-- Oscar Wilde
| |
| Erik Max Francis 2004-02-16, 10:34 am |
| joe@invalid.address wrote:
> spam_bait101@yahoo.com (sb) writes:
>
>
> This isn't true. You can derive a streambuf class that will take a
> FILE* or file descriptor as a constructor parameter, and use that to
> get/send data. Josuttis gives an example of the latter in his book.
And, in fact, practically any Unix-based C++ Standard Library is going
to have these available as extensions. Simply check your compiler
manual.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Seriousness is the only refuge of the shallow.
-- Oscar Wilde
| |
| joe@invalid.address 2004-02-16, 11:34 am |
| Barry Margolin <barmar@alum.mit.edu> writes:
> In article <m3d68e3f99.fsf@invalid.address>, joe@invalid.address wrote:
>
>
> I understand that file descriptors wouldn't be portable, but why not
> FILE*? It's part of the C standard, and C++ incorporates most of C
> by reference.
I probably misspoke there. As you say, it should be portable to
everywhere that C is (allowing for embedded environments that don't
have struct FILE, etc). I've seen an example of a class that wrapped
popen(), and it was more trouble than it was worth to me. YMMV.
Joe
--
"I didn't really say everything I said."
- Yogi Berra
| |
| Erik Max Francis 2004-02-16, 1:34 pm |
| Erik Max Francis wrote:
> I think he meant wrapping a streambuf around a FILE * wouldn't be
> portable, ...
"Portable" wasn't the right word, here; I meant part of the Standard
Library, not portable. i.e., if you were using such a thing without
writing it yourself, you're relying on an implementation extension, not
a Standard feature.
--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ I have not yet begun to right!
-- John Paul Jones
|
|
|
|
|