473,383 Members | 1,868 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,383 software developers and data experts.

Clear a stringstream

This is a subtle variation of a question I posted some time ago.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();
s.str().erase();
std::cout << "s=" << s.str();
s.str( "" );
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following output:

s=
s=Hello, world!
s=Hello, world!
Exception catch: unexpected NULL pointer in function: basic_string( const charT* ,size_type,const Allocator&)

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
6 7919
Christopher Benson-Manica wrote:
This is a subtle variation of a question I posted some time ago.

#include <map>
I don't think anything from that header is used in your program.
#include <iostream>
#include <sstream>
Add

#include <string>
#include <exception>

(just in case)

int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();
s.str().erase();
Why are you calling 'erase()' here? Haven't you been told to do

s.str(std::string());

?
std::cout << "s=" << s.str();
s.str( "" );
That should work fine too.
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following output:

s=
s=Hello, world!
s=Hello, world!
Exception catch: unexpected NULL pointer in function: basic_string( const charT* ,size_type,const Allocator&)

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?


I compiled your code (with my corrections) with VC++ v6 (not the best
compiler/library combo out there), and it ran fine (and no exceptions
occurred).

What _is_ that implementation? Please tell us so we could avoid it
like the plague. And get yourself a decent one, before it's too late.

Victor
Jul 22 '05 #2
Christopher Benson-Manica wrote in news:c9**********@chessie.cirr.com in
comp.lang.c++:
This is a subtle variation of a question I posted some time ago.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();
This erases a *copy* of the sequence in 's':
s.str().erase();
To reset the sequence use:

s.str( "" );
std::cout << "s=" << s.str();
s.str( "" );
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following
output:

s=
s=Hello, world!
s=Hello, world!
All as expect so far.
Exception catch: unexpected NULL pointer in function: basic_string(
const charT* ,size_type,const Allocator&)
Probably as you erase()'d the temporary returned by str(), but it
shouldn't happen.

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?


if str( "" ) doesn't work try str( " " ) (note the space) I've found
1 implementation where this was required.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?


Hahahahahahahaha, so much for the stringstream * idea.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::ostringstream *os=new std::ostringstream();
std::cout << "Hello, world!\n";
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

This seemingly trivial program crashes (doesn't even throw the
exception, and makes Windows try to send an error report) when linked
with the company library that every program must use.

I'm going to get my resume updated this weekend.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #4
Christopher Benson-Manica wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?

Hahahahahahahaha, so much for the stringstream * idea.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::ostringstream *os=new std::ostringstream();
std::cout << "Hello, world!\n";
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

This seemingly trivial program crashes (doesn't even throw the
exception, and makes Windows try to send an error report) when linked
with the company library that every program must use.


I know that if compiled by itself by VC++ v6 and then executed,
it doesn't do that and simply displays "Hello, world!"

Could it be that the "company library" has a faulty memory manager
in it that chokes on 'new std::ostringstream()' somehow?...

I'm going to get my resume updated this weekend.


Never a bad idea to keep it up to date. Speaking from experience
here :-(

V
Jul 22 '05 #5
Victor Bazarov <v.********@comacast.net> spoke thus:
I don't think anything from that header is used in your program.
My mistake.
Why are you calling 'erase()' here? Haven't you been told to do
s.str(std::string());
It doesn't work :(
s.str( "" );

That should work fine too.
I know it...
What _is_ that implementation? Please tell us so we could avoid it
like the plague. And get yourself a decent one, before it's too late.


Borland C++ compiler 5.4. 5.5.1 compiles the code correctly, but
compiling all our 5.4 code is a major hassle that I haven't resolved
yet. I haven't gotten a good answer for why we're two versions behind
the current C++ Builder, besides the fact that no one wants to make
the transition.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #6
Rob Williscroft <rt*@freenet.co.uk> spoke thus:
To reset the sequence use:
s.str( "" );
Except it doesn't work here...
if str( "" ) doesn't work try str( " " ) (note the space) I've found
1 implementation where this was required.


That was the hack I vaguely remembered but couldn't find in the Google
archives. Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #7

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

Similar topics

2
by: Bill Beacom | last post by:
Hi all, I am trying to use stringstream to assemble a text message from a set of user-defined objects that have the insertion operator overloaded. It seems to be putting them into the...
3
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
1
by: kathy | last post by:
I am trying to convert integer to std::string by using: int m,n; std::string s1,s2; std::stringstream sStream; sStream << m; s1 = sStream.str(); ????? how to clear up sStream ???
9
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
9
by: martinezfive | last post by:
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");
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.