Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how can i convert a file stream in a string?

Reply
Thread Tools

how can i convert a file stream in a string?

 
 
Manuel
Guest
Posts: n/a
 
      09-11-2006
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

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      09-11-2006

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.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-12-2006
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
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      09-12-2006
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:stringstream out;

out << in.rdbuf();

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

return 0;
}

Hope this helps,
Nate
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      09-12-2006
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
 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      09-12-2006
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

 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      09-12-2006
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.

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-12-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 1 04-17-2007 09:15 AM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments