Connecting Tech Pros Worldwide Help | Site Map

FILE * and fstream

  #1  
Old November 14th, 2006, 08:35 PM
Adrian
Guest
 
Posts: 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!
  #2  
Old November 14th, 2006, 10:15 PM
BobR
Guest
 
Posts: n/a

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


  #3  
Old November 15th, 2006, 03:55 PM
Tilman Kuepper
Guest
 
Posts: n/a

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



  #4  
Old November 15th, 2006, 08:05 PM
Adrian
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoid duplicates at the end of the file during read Andr3w answers 1 March 4th, 2008 02:18 AM
appending data to binary file fstream rory answers 10 January 3rd, 2008 06:15 PM
File I/O with fstream emaghero answers 1 February 21st, 2007 04:33 PM
Text file Program help please ComicCaper answers 5 May 13th, 2006 09:25 PM
vector of fstream, does not work? Someonekicked answers 9 April 18th, 2006 06:15 PM