468,463 Members | 1,957 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

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 19215

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jk | last post: by
10 posts views Thread by Nikolay Petrov | last post: by
3 posts views Thread by Ursula | last post: by
3 posts views Thread by tparks69 | last post: by
2 posts views Thread by Robert Dufour | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.