473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

FILE * and fstream

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
3 2578

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
2
by: Brian Ward | last post by:
First: sorry as a relative newbie for previously not including code. My question: Reading C++ books I almost always find programs such as the one below give the following type of code for reading...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
1
by: lkr | last post by:
hi i got one file transfer program using serialization which has a limitation that i can send only 8192 bytes(8KB). i want to send more than that wht can i do. how can i divide the file into...
5
by: Eric Sabine | last post by:
I have a web site where the user is able to download a file. I can't use an anchor tag because I don't want the user to see where the filename is coming from. Below is the code in which the user...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
5
by: Dic4000 | last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË? Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷. #include<iostream> #include<conio.h> #include<fstream> using namespace std;
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
1
Andr3w
by: Andr3w | last post by:
Hi, I was working on something when I noticed that the following code produced a duplicate char before reaching at the end of the file if it had a blank line (with no chars at the file) before...
2
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.