473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

stringstream overwrite with constructor vs. <<

Hi,
I feel no doubt some documentation contains my answer, so bare with
me. Given the following:

#inclde <stdio.h>
#include <sstream>

void f()
{
std::stringstream a("Hello World!\n");
a << "Bite me!\n";
printf( a.str().c_str() );
}

I expect to see:

Hello World!
Bite me!

on the console. However, I instead find:

Bite me!
ld!

no matter what compiler/library combination I use. As I mentioned,
surely, the answer to "Why?" appears somewhere, I simply remain unable
to find it. Would someone point me in the right direction for the
answer?

Jan 4 '07 #1
9 5488
ma**********@comcast.net wrote:
Hi,
I feel no doubt some documentation contains my answer, so bare with
me. Given the following:

#inclde <stdio.h>
#include <sstream>

void f()
{
std::stringstream a("Hello World!\n");
a << "Bite me!\n";
printf( a.str().c_str() );
}

I expect to see:

Hello World!
Bite me!

on the console. However, I instead find:

Bite me!
ld!

no matter what compiler/library combination I use. As I mentioned,
surely, the answer to "Why?" appears somewhere, I simply remain unable
to find it. Would someone point me in the right direction for the
answer?
I think you either need to open your stream for appending, or position
it at its end before writing.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 4 '07 #2
Victor Bazarov wrote:
ma**********@comcast.net wrote:
>Hi,
I feel no doubt some documentation contains my answer, so bare
with me. Given the following:

#inclde <stdio.h>
#include <sstream>

void f()
{
std::stringstream a("Hello World!\n");
a << "Bite me!\n";
printf( a.str().c_str() );
}

I expect to see:

Hello World!
Bite me!

on the console. However, I instead find:

Bite me!
ld!

no matter what compiler/library combination I use. As I mentioned,
surely, the answer to "Why?" appears somewhere, I simply remain
unable to find it. Would someone point me in the right direction
for the answer?

I think you either need to open your stream for appending, or
position it at its end before writing.
Yes, a stream only has one 'current position', which is either a read
position or a write position. When you construct a stream, it cannot know if
the next operation will be a read or a write, so unless specifically
instructed the 'current position' is set at the start of the stream.
Bo Persson
Jan 4 '07 #3
Bo Persson wrote:
Victor Bazarov wrote:
ma**********@comcast.net wrote:
Hi,
I feel no doubt some documentation contains my answer, so bare
with me. Given the following:

#inclde <stdio.h>
#include <sstream>

void f()
{
std::stringstream a("Hello World!\n");
a << "Bite me!\n";
printf( a.str().c_str() );
}

I expect to see:

Hello World!
Bite me!

on the console. However, I instead find:

Bite me!
ld!

no matter what compiler/library combination I use. As I mentioned,
surely, the answer to "Why?" appears somewhere, I simply remain
unable to find it. Would someone point me in the right direction
for the answer?
I think you either need to open your stream for appending, or
position it at its end before writing.

Yes, a stream only has one 'current position', which is either a read
position or a write position. When you construct a stream, it cannot know if
the next operation will be a read or a write, so unless specifically
instructed the 'current position' is set at the start of the stream.
Bo Persson
Do you know of the particular portion of the ANSI C++ Standard to cite
describing the 'current position' characteristic? My copy does not
make such a statement, as best I can tell.

Jan 4 '07 #4
ma**********@comcast.net wrote:
[..]
Do you know of the particular portion of the ANSI C++ Standard to cite
describing the 'current position' characteristic? My copy does not
make such a statement, as best I can tell.
I am not sure what to cite except the whole of clause 27. The Standard
document is not an instruction manual from which you can learn how to
program. You will be much better off with a good book.

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

ma**********@comcast.net wrote in message ...
>
Do you know of the particular portion of the ANSI C++ Standard to cite
describing the 'current position' characteristic? My copy does not
make such a statement, as best I can tell.
No, I don't. But this works for me:

{
std::stringstream ass("Hello World!\n");
ass.seekp(0, std::ios_base::end);
ass << "Bite me!\n";
// printf( ass.str().c_str() );
cout<<" stringstream ass test ="<<ass.str().c_str()<<std::endl;
}
/* - output -
stringstream ass test =Hello World!
Bite me!
*/

--
Bob R
POVrookie
Jan 4 '07 #6

Victor Bazarov wrote:
ma**********@comcast.net wrote:
[..]
Do you know of the particular portion of the ANSI C++ Standard to cite
describing the 'current position' characteristic? My copy does not
make such a statement, as best I can tell.

I am not sure what to cite except the whole of clause 27. The Standard
document is not an instruction manual from which you can learn how to
program. You will be much better off with a good book.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I agree a good book provides better instruction on learning to program.
However, in my line of work, I need to know the ins and outs of the
Standard Library, prompting my concern with the ANSI Standard.

Looking through Clause 27, we see the '<<' *should* insert the argument
on the RHS into the one on the LHS. The fact the constructor
initializes the stringstream's internal buffer leads me to expect the
subsequent '<<' will append (not overwrite) the RHS to the buffer of
the stringstream. However, after checking Borland, Edison Design
Group, GCC, and MS 6, 7 and 8 (all six in both strict and non-strict
modes), I found each one of these compilers and their Standard Library
implementations produce the (from my POV) non-intuitive behavior.

As I said, I feel confident the rationale for this output exists
*somewhere*. However, I have not found it in the documentation of any
of my compilers or their libraries, leaving me with my original
question: Why?

Jan 4 '07 #7
ma**********@comcast.net wrote:
[..]
Looking through Clause 27, we see the '<<' *should* insert the
argument on the RHS into the one on the LHS.
It does. At the predefined location in the buffer. Here, the
location is the beginning of the buffer, not its end.
The fact the constructor
initializes the stringstream's internal buffer leads me to expect the
subsequent '<<' will append (not overwrite) the RHS to the buffer of
the stringstream.
Why? "Insert" doesn't mean "append".
However, after checking Borland, Edison Design
Group, GCC, and MS 6, 7 and 8 (all six in both strict and non-strict
modes), I found each one of these compilers and their Standard Library
implementations produce the (from my POV) non-intuitive behavior.
It cannot be [non-]intuitive from anybody's single POV. It's either
intuitive or not, meaning that the majority of sapient beings expect
it to behave in a certain manner.
As I said, I feel confident the rationale for this output exists
*somewhere*. However, I have not found it in the documentation of any
of my compilers or their libraries, leaving me with my original
question: Why?
Trace all explanations from the 'operator<<' to 'sputc'. Start in
27.6.2.5.4. 27.6.2 is where 'sputc' is mentioned. It's not easy,
I give you that. Reading the Standard is not for impatient. That's
why a good book helps, and hopefully mentions the Standard as well.

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

ma**********@comcast.net wrote in message ...
>
Looking through Clause 27, we see the '<<' *should* insert the argument
on the RHS into the one on the LHS. The fact the constructor
initializes the stringstream's internal buffer leads me to expect the
subsequent '<<' will append (not overwrite) the RHS to the buffer of
the stringstream.
{
std::stringstream ass("Hello World!\n");
std::cout<<" ass test tellp="<<ass.tellp()<<std::endl;
std::cout<<" ass test tellg="<<ass.tellg()<<std::endl;
}

You'll see that the stream is initialised with the 'get' and 'put' pointers
set to zero.

However, after checking Borland, Edison Design
Group, GCC, and MS 6, 7 and 8 (all six in both strict and non-strict
modes), I found each one of these compilers and their Standard Library
implementations produce the (from my POV) non-intuitive behavior.
I think it is reasonable.
>
As I said, I feel confident the rationale for this output exists
*somewhere*. However, I have not found it in the documentation of any
of my compilers or their libraries, leaving me with my original
question: Why?
If you want to 'Append To End' (ate), tell the stream that!

{
std::stringstream ass( "Hello World!\n", std::ios::out |
std::ios::ate );

std::cout<<" ass test tellp="<<ass.tellp()<<std::endl;
// std::cout<<" ass test tellg="<<ass.tellg()<<std::endl;
// ass.seekp(0, std::ios_base::end);
ass << "Bite me!\n";
// printf( ass.str().c_str() );
std::cout<<" stringstream ass test ="<<ass.str().c_str()<<std::endl;
}

If you use 'std::ios::app', you can't re-position the ostream pointer, it
will always return to the end.

--
Bob R
POVrookie
Jan 5 '07 #9
ma**********@comcast.net wrote:
Bo Persson wrote:
>Victor Bazarov wrote:
>>ma**********@comcast.net wrote:
Hi,
I feel no doubt some documentation contains my answer, so bare
with me. Given the following:

#inclde <stdio.h>
#include <sstream>

void f()
{
std::stringstream a("Hello World!\n");
a << "Bite me!\n";
printf( a.str().c_str() );
}

I expect to see:

Hello World!
Bite me!

on the console. However, I instead find:

Bite me!
ld!

no matter what compiler/library combination I use. As I
mentioned, surely, the answer to "Why?" appears somewhere, I
simply remain unable to find it. Would someone point me in the
right direction for the answer?

I think you either need to open your stream for appending, or
position it at its end before writing.

Yes, a stream only has one 'current position', which is either a
read position or a write position. When you construct a stream, it
cannot know if the next operation will be a read or a write, so
unless specifically instructed the 'current position' is set at
the start of the stream.
Bo Persson

Do you know of the particular portion of the ANSI C++ Standard to
cite describing the 'current position' characteristic? My copy
does not make such a statement, as best I can tell.
The formal term is actually "the stream position", given in section 27.5.1
on Stream buffer requirements.

Section 27.7.1.1 describes the constructors for basic_stringbuf, which holds
the content of a stringstream. It says:

"explicit basic_stringbuf(const basic_string<charT,traits,Allocator>& str ,
ios_base::openmode which = ios_base::in |
ios_base::out);

Effects: Constructs an object of class basic_stringbuf, initializing the
base class with basic_streambuf() (27.5.2.1), and initializing mode with
which . Then copies the content of str into the basic_stringbuf underlying
character sequence. If which & ios_base::out is true, initializes the output
sequence such that pbase() points to the first underlying character, epptr()
points one past the last underlying character, and pptr() is equal to
epptr() if which & ios_base::ate is true, otherwise pptr() is equal to
pbase(). If which & ios_base::in is true, initializes the input sequence
such that eback() and gptr() point to the first underlying character and
egptr() points one past the last underlying character."
Here pptr() is the "put pointer" indicating the next position to write.
Default is to have pptr() and gptr() start at the beginning of the buffer.

Bo Persson
Jan 5 '07 #10

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

Similar topics

2
by: BeEnola | last post by:
Hi! I have a result of a Java-Library that looks similar to <td class="xyz"><nobr>TEXT</nobr></td> I am not able to change this coding. But has anybody an idea to define another tag - outside...
17
by: pub | last post by:
When creating a list: list<class A*> l; How to delete all the objects whose pointers are contained in "l"? Thanks for your comments?
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
3
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
5
by: probstm | last post by:
I am using a message handler class that implements amongst others: static void message ( std::string aHeader, std::string aMessage, int aTimeout = 0, MessageType aFlag = MSG_Information ); ...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.