473,386 Members | 1,867 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,386 software developers and data experts.

Get FILE * pointer from C++ stream?

Can I get a dumb FILE * pointer from a C++ stream?

#include <iostream>
#include <sstream>

using namespace std;

int main(){
stringstream my_stream;
my_stream << "Hello World!" << endl;
// FILE *p = ???;
}

The reason is that I work with a C library that needs a FILE pointer,
but I am calling it from C++ and want to pass a string, actually. I
cannot use fmemopen, because it is not portable (only GNU).

Thanks
Markus
Jul 22 '05 #1
3 4604

"Markus Dehmann" <ma*******@gmx.de> wrote in message
news:c1**************************@posting.google.c om...
Can I get a dumb FILE * pointer from a C++ stream?

#include <iostream>
#include <sstream>

using namespace std;

int main(){
stringstream my_stream;
my_stream << "Hello World!" << endl;
// FILE *p = ???;
}
Unfortunately, cstdio is not extensible the way C++ iostreams are. You
can't cook up a FILE to represent an arbitrary data source/sink.
The reason is that I work with a C library that needs a FILE pointer, but I am calling it from C++ and want to pass a string, actually. I
cannot use fmemopen, because it is not portable (only GNU).


Write the string to a temporary file. This may be inefficient, but
there may be no other way.

Jonatan
Jul 22 '05 #2
Markus Dehmann wrote:
Can I get a dumb FILE * pointer from a C++ stream?
What do you mean by "dumb"? Anyway, if you mean a standard [io]fstream,
the answer is no, you can't. Most won't even use FILE* internally, but
rather the OS facitily blow that is also used by FILE*.
#include <iostream>
#include <sstream>

using namespace std;

int main(){
stringstream my_stream;
my_stream << "Hello World!" << endl;
// FILE *p = ???;
}


Huh? How is a stringstream connected to a FILE*? A stringstream writes
to or reads from a string, not a file.

Jul 22 '05 #3
"Markus Dehmann" <ma*******@gmx.de> wrote in message
news:c1**************************@posting.google.c om...
Can I get a dumb FILE * pointer from a C++ stream?


Only if your C++ implementation happens to work that way. There is no
guarantee that a C++ stream will be implemented in terms of a C FILE pointer
at all, let alone that there is a way to discover it.
Jul 22 '05 #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 |...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
11
by: hilz | last post by:
Hi all: I have a FILE pointer that points to a file on disk (not any other io stream). is there a way to get the filename of that FILE? i thought it would be the _tmpfname part of the struct, but...
8
by: Ritesh | last post by:
Hi!!!!!!!!!!! I am a final year engineering student. I am making a final year project on reed-solomon codec in C using VC++ 6.0. The input to the program can be any file that the user wants to...
32
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
2
by: Senthil | last post by:
Hi, The need is to read each line in a text file and based on the read data decision has to be taken whether to reposition the file pointer. The StreamReader.Position does not give current...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.