C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FILE *fd to string

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
va
Guest





PostPosted: Mon May 07, 2007 11:25 pm    Post subject: FILE *fd to string Reply with quote



I have a C style function which is of the form writestuff(FILE *fd).
Now this function writes strings to the FILE* provided. Is there a way
to collect this output into a string (without the obvious creating and
reading of a temp file?). I just want the output of the 'writestuff'
function to be collected into a std::string. I can't modify the
writestuff function to do the needful. Any suggestions?
Thanks,


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Sebastian Redl
Guest





PostPosted: Tue May 08, 2007 1:05 am    Post subject: Re: FILE *fd to string Reply with quote



On Mon, 7 May 2007, va wrote:

Quote:
I have a C style function which is of the form writestuff(FILE *fd).
Now this function writes strings to the FILE* provided. Is there a way
to collect this output into a string (without the obvious creating and
reading of a temp file?). I just want the output of the 'writestuff'
function to be collected into a std::string. I can't modify the
writestuff function to do the needful. Any suggestions?
Thanks,

Not in a portable way, as FILE is opaque.
There are some non-portable ways.

For example, with knowledge of your CRT, you can construct a FILE
structure that writes into a memory buffer. (That's what the MS CRT does
for sprintf, for example.)

With knowledge of your platform, you can e.g. create a POSIX pipe and use
fdopen() to turn the write end of the pipe into a FILE*, then read the
strings from the other end.

But nothing portable, I'm afraid.

Sebastian Redl

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.