473,666 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialize a string or a stringstream with fstream

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...

Jun 29 '06 #1
14 4436
cp***@austin.rr .com 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.dat a(), s.size());

(or something like that) ??
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #2

Victor Bazarov wrote:
cp***@austin.rr .com 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.dat a(), s.size());

(or something like that) ??
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


string::data() appears to be const

Jun 29 '06 #3
cp***@austin.rr .com wrote:
Victor Bazarov wrote:
cp***@austin.rr .com 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.dat a(), s.size());

(or something like that) ??
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #4

Victor Bazarov wrote:
cp***@austin.rr .com wrote:
Victor Bazarov wrote:
cp***@austin.rr .com 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.dat a(), s.size());

(or something like that) ??

[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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.

Jun 29 '06 #5
cp***@austin.rr .com 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?

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_iter ator. See Item 29 (p. 126) of Scott Meyers' "Effective
STL" for an example.

--
Paul M. Dubuc
Jun 29 '06 #6
cp***@austin.rr .com wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #7

Victor Bazarov wrote:
cp***@austin.rr .com wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


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(bu ffer, 512);
string mystring("buffe r);
}

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

Jun 29 '06 #8

Paul M. Dubuc wrote:
cp***@austin.rr .com 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?

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_iter ator. See Item 29 (p. 126) of Scott Meyers' "Effective
STL" for an example.

--
Paul M. Dubuc


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?

Jun 29 '06 #9

cp***@austin.rr .com wrote:
Paul M. Dubuc wrote:
cp***@austin.rr .com 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?

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_iter ator. See Item 29 (p. 126) of Scott Meyers' "Effective
STL" for an example.

--
Paul M. Dubuc


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(input File);
std::istreambuf _iterator<char> dataEnd;
std::string fileData(dataBe gin, dataEnd);

Jun 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8689
by: Dmitri Zhukov | last post by:
Hi all, I've found out a difference between stringstream and ifstream objects: stringstream::getline() will include \r (13) character ifstream::getline won't include \r I wonder how come. Is it prescribed behavior?
19
2475
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a temporary. Here is a tiny test programm: #include <iostream> #include <sstream>
5
5959
by: Bernhard Hidding | last post by:
Hello, I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why does this piece of code not work? #include <iostream.h> #include <stdio.h> #include <fstream.h> #include <string> .... intvar intg = 0;
6
2742
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
2
12235
by: Ney André de Mello Zunino | last post by:
Hello. Is g++ 3.3.3 (Cygwin) correct in rejecting the following test program? #include <istream> #include <sstream> #include <fstream> void test(std::istream& source) {
6
8137
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional character array, but I'd really like to read it into a vector of strings. Here's how I'm doing the read into the char array: int main() { string str1("booger"); string str2("test");
9
9171
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
4
3929
by: nnandini8 | last post by:
Hello guys, I have a small doubt in istream. I have a file calles infile.txt infile.txt contains Hello World now I wanted to add these contents to a string. ie i need to have hello world in a string Note there is a space between Hello and WOrld Any help will be greatly appreciated Thank You
12
2751
by: grubbymaster | last post by:
hello i'm extracting from a file numbers that are stored on each line. this is the only way i know to extract each line at a time from a file , using <fstream> <ostream> : char buffer; ifstream LZW(sz_path);
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.