Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Moving text file contents into a std::string

Reply
Thread Tools

Moving text file contents into a std::string

 
 
Dave
Guest
Posts: n/a
 
      05-20-2004

Hello all,

The scheme shown below to move a text file's contents into a std::string
works with one exception: it drops the carriage return and line feed
characters. How may I, in a Standard-compliant way, read in a text file's
contents and keep the carriage returns and line feeds?

Thanks,
Dave

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

using namespace std;

//
************************************************** **************************
*
//
************************************************** **************************
*
//
************************************************** **************************
*
int main()
{
string file_contents;
ifstream file_stream("test_1.txt");

copy(
istream_iterator<char>(file_stream),
istream_iterator<char>(),
back_inserter(file_contents)
);

cout << file_contents << endl;
} // main



 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      05-20-2004
Dave wrote:
> Hello all,
>
> The scheme shown below to move a text file's contents into a std::string
> works with one exception: it drops the carriage return and line feed
> characters. How may I, in a Standard-compliant way, read in a text file's
> contents and keep the carriage returns and line feeds?
>
> Thanks,
> Dave
>
> #include <algorithm>
> #include <fstream>
> #include <iostream>
> #include <iterator>
> #include <string>
>
> using namespace std;
>
> //
> ************************************************** **************************
> *
> //
> ************************************************** **************************
> *
> //
> ************************************************** **************************
> *
> int main()
> {
> string file_contents;
> ifstream file_stream("test_1.txt");
>
> copy(
> istream_iterator<char>(file_stream),
> istream_iterator<char>(),
> back_inserter(file_contents)
> );
>
> cout << file_contents << endl;
> } // main
>
>
>


Use the following to deactivate skipping whitespace:

file_stream.setf(0, ios::skipws) ;


I wouldn't expect that one would need to do this. But it seems it is
necessary in my implementation as well. Anyone have any comments about
what the standard says about this?

Alan
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      05-20-2004
On Thu, 20 May 2004 11:35:18 -0700 in comp.lang.c++, "Dave" <> wrote,
>characters. How may I, in a Standard-compliant way, read in a text file's
>contents and keep the carriage returns and line feeds?


Maybe enough to get you going:
file_stream.unsetf(ios::skipws);

Please read:
http://groups.google.com/groups?selm...ing.google.com
http://groups.google.com/groups?selm...ing.google.com

 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      05-21-2004
"Dave" <> wrote in message news:<>...
> Hello all,
>
> The scheme shown below to move a text file's contents into a std::string
> works with one exception: it drops the carriage return and line feed
> characters. How may I, in a Standard-compliant way, read in a text file's
> contents and keep the carriage returns and line feeds?


int main()
{
std::string file_contents;
std::ifstream file_stream("test_1.txt");

std:stringstream temp;
temp << file_stream.rdbuf();

file_contents = temp.str();
std::cout << file_contents << std::endl;
return 0;
}

This also has the bonus that it's usually quite a bit faster.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-21-2004
"Dave" <> wrote in message

> The scheme shown below to move a text file's contents into a std::string
> works with one exception: it drops the carriage return and line feed
> characters. How may I, in a Standard-compliant way, read in a text file's
> contents and keep the carriage returns and line feeds?


> string file_contents;
> ifstream file_stream("test_1.txt");
>
> copy(
> istream_iterator<char>(file_stream),
> istream_iterator<char>(),
> back_inserter(file_contents)
> );


Does opening the file in binary mode fix it?

ifstream file_stream("test_1.txt", ios::binary);
 
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
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
appending the contents of multiple text files into 1 file Paul Danese Ruby 1 06-14-2007 04:25 PM
Screen scraping an html text contents into a file basi Ruby 15 12-07-2005 04:44 PM
Reading a small text file contents into an array Foxy Kav C++ 6 04-28-2004 01:52 PM
Load contents of a text file into a text box Jeremy Chapman ASP .Net 1 08-15-2003 10:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57