Moving text file contents into a std::string

D

Dave

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
 
A

Alan Johnson

Dave said:
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
 
J

Jerry Coffin

Dave said:
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::eek: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.
 
S

Siemel Naran

Dave said:
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);
 

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

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top