std::string from file input

  • Thread starter Alexandros Frantzis
  • Start date
A

Alexandros Frantzis

Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis
 
M

Mike Wahler

Alexandros Frantzis said:
Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string filename("myfile");

std::ifstream ifs(filename.c_str());
if(!ifs)
{
std::cerr << "Cannot open file '" << filename << "'\n";
return EXIT_FAILURE;
}

std::eek:stringstream oss;
oss << ifs.rdbuf();

if(!ifs && !ifs.eof())
std::cerr << "Error reading file '" << filename << "'\n";

std::string contents(oss.str());
std::cout << "file_contents:\n" << contents << '\n';

return 0;
}


-Mike
 
T

Thomas Matthews

Alexandros said:
Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis

I would never read an entire file into a string, since
files are notorisly bigger than a computers memory.

One method is to use a std::vector and ifstream::read().
The std::vector will expand as needed. There are ways to
optimize this by reserving extra space.

Is there a need to read the entire file into memory?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jerry Coffin

Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

The usual way is to read from the stream's buffer into a stringstream:

std::eek:stringstream buffer;
buffer << input.rdbuf();

at this point, buffer.str() is an std::string containing the entire
contents of the file.
 

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
474,161
Messages
2,570,892
Members
47,426
Latest member
MrMet

Latest Threads

Top