Connecting Tech Pros Worldwide Help | Site Map

FILE * and fstream

Adrian
Guest
 
Posts: n/a
#1: Nov 14 '06
Does anyone has some advice on how to use C FILE *'s safely in C++

I have c style library functions that require a FILE * but I would prefer the exception safe fstreams. Is there anyway to create a stream from a FILE * or get the FILE * from a stream?

I seem to remember a nice way of wrapping FILE * in a class from Herb Sutters books, but I dont have them with me.



TIA

--

Adrian

Think you know a language? Post to comp.lang... and find out!
BobR
Guest
 
Posts: n/a
#2: Nov 14 '06

re: FILE * and fstream



Adrian wrote in message ...
Quote:
>Does anyone has some advice on how to use C FILE *'s safely in C++
>
>I have c style library functions that require a FILE * but I would prefer
the exception safe fstreams. Is there anyway to create a stream from a FILE *
or get the FILE * from a stream?
Quote:
>
>I seem to remember a nice way of wrapping FILE * in a class from Herb
Sutters books, but I dont have them with me.
Quote:
>

I found this in an **old** GNU C++ iostreams doc:

<">
"C Input and Output"

libio is distributed with a complete implementation of the ANSI C stdio
facility. It is implemented using streambuf objects. See section Wrappers for
C stdio.
// .....

Extensions beyond ANSI:

* A stdio FILE is identical to a streambuf. Hence there is no need to worry
about synchronizing C and C++ input/output--they are by definition always
synchronized.

* If you create a new streambuf sub-class (in C++), you can use it as a FILE
from C. Thus the system is extensible using the standard streambuf protocol.

"Wrappers for C stdio"

A stdiobuf is a streambuf object that points to a FILE object (as defined by
stdio.h). All streambuf operations on the stdiobuf are forwarded to the FILE.
Thus the stdiobuf object provides a wrapper around a FILE, allowing use of
streambuf operations on a FILE. This can be useful when mixing C code with
C++ code.
// .......
</">

Don't know if that could help you. Maybe it could narrow your search.

--
Bob R
POVrookie


Tilman Kuepper
Guest
 
Posts: n/a
#3: Nov 15 '06

re: FILE * and fstream


Hi Adrian,
Quote:
I seem to remember a nice way of wrapping FILE * in a class
from Herb Sutters books, but I dont have them with me.
I think it was in Josuttis' book. See here:
http://www.josuttis.com/cppcode/fdstream.html

Best regards,
Tilman



Adrian
Guest
 
Posts: n/a
#4: Nov 15 '06

re: FILE * and fstream


Tilman Kuepper wrote:
Quote:
Hi Adrian,
>
Quote:
>I seem to remember a nice way of wrapping FILE * in a class
>from Herb Sutters books, but I dont have them with me.
>
I think it was in Josuttis' book. See here:
http://www.josuttis.com/cppcode/fdstream.html
Now that I have with me :-) I have been trawling through it, will
check it out.

Thanks


--

Adrian

Think you know a language? Post to comp.lang... and find out!
Closed Thread