469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Moving text file contents into a std::string


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

Jul 22 '05 #1
4 2409
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
Jul 22 '05 #2
On Thu, 20 May 2004 11:35:18 -0700 in comp.lang.c++, "Dave" <be***********@yahoo.com> 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/gr*********...ing.google.com
http://groups.google.com/gr*********...ing.google.com

Jul 22 '05 #3
"Dave" <be***********@yahoo.com> wrote in message news:<10*************@news.supernews.com>...
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::ostringstream 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.
Jul 22 '05 #4
"Dave" <be***********@yahoo.com> 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);
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Angus Leeming | last post: by
22 posts views Thread by Jason Heyes | last post: by
6 posts views Thread by Khuong Dinh Pham | last post: by
25 posts views Thread by electrixnow | last post: by
6 posts views Thread by SpreadTooThin | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.