initialize a string or a stringstream with fstream

C

cpisz

I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the entire
file contents)

my problem now is how can i grab all the file contents in one swoop and
put it in a string for editing?

I hate to go through the file and copy line by line:

string s = file.getline()
while(!file.eof)
{
s += file.getline();
}

That seems aweful inefficient...

isnt there some way to initialize the string with the contents of the
file?
like:
string s(file.some data buffer)?

If I copy the contents from a file to a string and then later from a
string to a stringstream and finally from a stringstream to individual
variables I am essentially copying the file 4 times, that could stink
for large files...
 
V

Victor Bazarov

I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the entire
file contents)

my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?

Couldn't you define the string to be of the size of the whole file and
then do

file.read(s.data(), s.size());

(or something like that) ??

V
 
C

cpisz

Victor said:
I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the entire
file contents)

my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?

Couldn't you define the string to be of the size of the whole file and
then do

file.read(s.data(), s.size());

(or something like that) ??

V

string::data() appears to be const
 
V

Victor Bazarov

Victor said:
I want to do some find and erase operations that i find in the
string class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the
entire file contents)

my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?

Couldn't you define the string to be of the size of the whole file
and then do

file.read(s.data(), s.size());

(or something like that) ??

V

string::data() appears to be const

Oh... Right... Well, then you're probably stuck with using 'getline'.
Have you tried giving it a different separator from '\n'? Like 0 or
something else non-existent? It should read the whole file in one
swoop...

V
 
P

Paul M. Dubuc

I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the entire
file contents)

my problem now is how can i grab all the file contents in one swoop and
put it in a string for editing?

I hate to go through the file and copy line by line:

string s = file.getline()
while(!file.eof)
{
s += file.getline();
}

That seems aweful inefficient...

isnt there some way to initialize the string with the contents of the
file?
like:
string s(file.some data buffer)?

If I copy the contents from a file to a string and then later from a
string to a stringstream and finally from a stringstream to individual
variables I am essentially copying the file 4 times, that could stink
for large files...

Use an istreambuf_iterator. See Item 29 (p. 126) of Scott Meyers' "Effective
STL" for an example.
 
C

cpisz

Victor said:
Victor said:
(e-mail address removed) wrote:
I want to do some find and erase operations that i find in the
string class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the
entire file contents)

my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?

Couldn't you define the string to be of the size of the whole file
and then do

file.read(s.data(), s.size());

(or something like that) ??

[..]

V

string::data() appears to be const

Oh... Right... Well, then you're probably stuck with using 'getline'.
Have you tried giving it a different separator from '\n'? Like 0 or
something else non-existent? It should read the whole file in one
swoop...

V

Yea, i can always get the whole file and put it in some char array
somewhere and then copy it over to a string, but my question is more
about the efficiency of doing that. I would like to skip the copy step
somehow and get directly into the string instead of having to copy from
some temporary char buffer.
 
V

Victor Bazarov

[..]
Yea, i can always get the whole file and put it in some char array
somewhere and then copy it over to a string, but my question is more
about the efficiency of doing that. I would like to skip the copy step
somehow and get directly into the string instead of having to copy
from some temporary char buffer.

If you question efficiency, you have to come up with hard numbers to
show that the ways available to you are inefficient somehow. For now
'std::getline' is it, and supposedly it does it as fast as possible
when 'std::string' is involved.

V
 
C

cpisz

Victor said:
[..]
Yea, i can always get the whole file and put it in some char array
somewhere and then copy it over to a string, but my question is more
about the efficiency of doing that. I would like to skip the copy step
somehow and get directly into the string instead of having to copy
from some temporary char buffer.

If you question efficiency, you have to come up with hard numbers to
show that the ways available to you are inefficient somehow. For now
'std::getline' is it, and supposedly it does it as fast as possible
when 'std::string' is involved.

V

There is no need for mathematical analysis...just comapre these two:
Proposal 1
{
Get x amount of data from file using getline and put it into a
char buffer
Copy x amount of data from char buffer to string
}

Proposal 2
{
Get x amount of data from file and put it into a string
}

getline requires you do this
{
char bufffer[512];
file.getline(buffer, 512);
string mystring("buffer);
}

See the differance? the question is if proposal 2 is possible or not.
I'll check into what Paul said.
 
C

cpisz

Paul said:
Use an istreambuf_iterator. See Item 29 (p. 126) of Scott Meyers' "Effective
STL" for an example.

Paul, I appreciate your helping, but I'd really hate to have to wait
for my paycheck and 2 weeks for shipping to find the answer to my
question. I don't see his text anywhere online for free viewing. Any
possibility of a small example?
 
C

cpisz

Paul, I appreciate your helping, but I'd really hate to have to wait
for my paycheck and 2 weeks for shipping to find the answer to my
question. I don't see his text anywhere online for free viewing. Any
possibility of a small example?

Ah nm. I finally found something useful on google now that I know to
search for istreambuf. the following code does the trick.

std::istreambuf_iterator<char> dataBegin(inputFile);
std::istreambuf_iterator<char> dataEnd;
std::string fileData(dataBegin, dataEnd);
 
V

Victor Bazarov

Victor said:
[..]
Yea, i can always get the whole file and put it in some char array
somewhere and then copy it over to a string, but my question is more
about the efficiency of doing that. I would like to skip the copy
step somehow and get directly into the string instead of having to
copy from some temporary char buffer.

If you question efficiency, you have to come up with hard numbers to
show that the ways available to you are inefficient somehow. For now
'std::getline' is it, and supposedly it does it as fast as possible
when 'std::string' is involved.

V

There is no need for mathematical analysis...just comapre these two:
Proposal 1
{
Get x amount of data from file using getline and put it into a
char buffer
Copy x amount of data from char buffer to string
}

Proposal 2
{
Get x amount of data from file and put it into a string

See 'std::getline'.
}

getline requires you do this
{
char bufffer[512];
file.getline(buffer, 512);
string mystring("buffer);
}

See the differance? the question is if proposal 2 is possible or not.
I'll check into what Paul said.

Also check out 'std::getline' (not 'std::istream::getline'). Pay
attention to the information provided to you _already_.

V
 
P

Paul M. Dubuc

Ah nm. I finally found something useful on google now that I know to
search for istreambuf. the following code does the trick.

std::istreambuf_iterator<char> dataBegin(inputFile);
std::istreambuf_iterator<char> dataEnd;
std::string fileData(dataBegin, dataEnd);

Yep! Sorry about being too lazy to post an example. Glad you found it.
Meyers says the implementation of an istreambuf_iterator is usually much
faster than an istream_iterator because they don't support formatted input.
Also, there's no need to allocate a sizeof file buffer or use getline() in a loop.
 
C

chandu

std::istream_iterator<char> istr(file);
std::istream_iterator<char> estr;

std::copy(istr, estr, std::back_inserter(s));
 
J

Johan Bengtsson

I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.

I made a function that will take a string (designed to hold the entire
file contents)

my problem now is how can i grab all the file contents in one swoop and
put it in a string for editing?

I hate to go through the file and copy line by line:

string s = file.getline()
while(!file.eof)
{
s += file.getline();
}

That seems aweful inefficient...

isnt there some way to initialize the string with the contents of the
file?
like:
string s(file.some data buffer)?

If I copy the contents from a file to a string and then later from a
string to a stringstream and finally from a stringstream to individual
variables I am essentially copying the file 4 times, that could stink
for large files...

The simplest solution is probably to use an istreambuf_iterator to
iterate over the contents of the file and then use the iterator form
of string construction.

-- 8< -- BEGIN SAMPLE CODE -- 8< --

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

int main()
{
std::ifstream in("stream.cc");

std::string str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());

std::cout << str << std::endl;

return 0;
}

-- 8< -- END SAMPLE CODE -- 8< --
 
J

Jerry Coffin

[ ... ]
my problem now is how can i grab all the file contents in one swoop and
put it in a string for editing?

I'm amazed that nobody has mentioned this:

std::ifstream infile(whatever);
std::stringstream buffer;

buffer << infile.rdbuf();

buffer now contains the entire contents of the file. If you want it
as a string, you can use buffer.str(). Oh, and yes, this is usually
more efficient (faster) than almost anything using std::getline.
 

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

Similar Threads

stringstream Problems 5
Write Stringstream in fstream ... 3
fstream Buffers 26
fstream File i/o 1
fstream tests 3
fstream 6
Why extracting string from stringstream(a) fails? 18
Bad use of stringstream temporary? 20

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top