By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

Moving text file contents into a std::string

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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.