473,406 Members | 2,439 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,406 software developers and data experts.

Write Stringstream in fstream ...

Hi,

I have a stringstream and want to write it 1:1 in a file.
Using this doesn't work:

stringstream myStream;
....
ofstream myFile(filename);
myFile<< myStream;
myFileclose();
I don't want to parse the stream line by line. Is there an easy way to
write the hole stream all at once in the file?

Thanks
Konrad
Jul 16 '07 #1
3 14224
Konrad Mühler wrote:
I have a stringstream and want to write it 1:1 in a file.
Using this doesn't work:

stringstream myStream;
...
ofstream myFile(filename);
myFile<< myStream;
Have you tried

myFile << myStream.str();
myFileclose();
I don't want to parse the stream line by line. Is there an easy way to
write the hole stream all at once in the file?
I am not sure what "easy" means.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 16 '07 #2
On 2007-07-16 08:15:18 -0400, Konrad Mühler
<mu*****@isg.cs.uni-magdeburg.desaid:
Hi,

I have a stringstream and want to write it 1:1 in a file.
Using this doesn't work:

stringstream myStream;
...
ofstream myFile(filename);
myFile<< myStream;
myFileclose();
myFile << myStream.rdbuf();

This works for all types of streams.

--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 16 '07 #3
myFile << myStream.rdbuf();
>
This works for all types of streams.
Thank you. This works fine :-)

Konrad
Jul 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
6
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
9
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
14
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.