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

how can i convert a file stream in a string?

Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?

Thanks,
bye

Sep 11 '06 #1
7 22882

Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
I don't understand the question.

Sep 11 '06 #2
Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
To initialize a string from a file, you can do:

#include <fstream>
#include <iostream>
#include <string>
#include <iterator>

int main ( void ) {
std::ifstream in_file ( "data.txt" );
std::string the_str ( std::istreambuf_iterator<char>( in_file ),
(std::istreambuf_iterator<char>()) );
std::cout << the_str;
}
Best

Kai-Uwe Bux
Sep 12 '06 #3
Manuel wrote:
Hi, I need to convert a text file in a string or char*. exists some
function that does it? or how can i do it?
If you're talking about copying the contents of a istream into a string,
then one way to do it is to use an ostringstream. You can insert the
streambuf pointer from the istream into the ostringstream and then get
the string from that. For example:

#include <fstream>
#include <string>
#include <sstream>

int main()
{
std::ifstream in("textfile.txt");
std::ostringstream out;

out << in.rdbuf();

std::string s = out.str();

return 0;
}

Hope this helps,
Nate
Sep 12 '06 #4
Kai-Uwe Bux wrote:
>
To initialize a string from a file, you can do:

[ snip std::istreambuf_iterator explanation ]
This of course is better than my solution. Thanks for reminding me
about this.

Nate
Sep 12 '06 #5
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?

best regard,
Manuel

Sep 12 '06 #6
Manuel wrote:
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?
Because it achieves the same without using an additional ostringstream
object. It's also a question of style. Some people just love using
iterators for everything. IMHO use whatever is clearer to you.

Regards,
Bart.

Sep 12 '06 #7
Bart wrote:
Manuel wrote:
First, thank you very much by your answers.

I decide to use the solution of Nate because it is more simple and
clearly and it make exactly as i need it.

But, why is kai-uwe's solution better than Nate's solution?

Because it achieves the same without using an additional ostringstream
object. It's also a question of style. Some people just love using
iterators for everything. IMHO use whatever is clearer to you.
Interestingly, it didn't compile under VC++ 6. Looks like the iterator
constructor that takes an istream isn't implemented.


Brian
Sep 12 '06 #8

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

Similar topics

5
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
6
by: hb | last post by:
Hi, Would you please give me some idea to convert/decode a Base 64 encoded GIF image string to a *.gif file in ASP.Net? Thank you hb
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: tparks69 | last post by:
I am getting data from an FTP into a Stream object. Now I need to put this data into a string so I can manipulate it. Can anyone show me some example code?
2
by: Robert Dufour | last post by:
I got a stream from a file, could be any document type like doc or txt. I want to convert it to a string variable so that I can replace some custom tag contents in it's text with text values...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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.