how can i convert a file stream in a string?

M

Manuel

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
 
N

Noah Roberts

Manuel said:
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.
 
K

Kai-Uwe Bux

Manuel said:
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
 
N

Nate Barney

Manuel said:
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::eek:stringstream out;

out << in.rdbuf();

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

return 0;
}

Hope this helps,
Nate
 
N

Nate Barney

Kai-Uwe Bux said:
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
 
M

Manuel

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
 
B

Bart

Manuel said:
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.
 
D

Default User

Bart said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top