By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,103 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,103 IT Pros & Developers. It's quick & easy.

FILE * and fstream

P: n/a
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!
Nov 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Adrian wrote in message ...
>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.
>

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
Nov 14 '06 #2

P: n/a
Hi Adrian,
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

Nov 15 '06 #3

P: n/a
Tilman Kuepper wrote:
Hi Adrian,
>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!
Nov 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.