Hello,
I am trying to write this simple code:
std::ostringstr eam s;
s << 1024;
std::cout << s.str() << std::endl;
s.str(""); // <- problem
s << 512;
std::cout << s.str() << std::endl;
The problem is that behind the scene I have a limited implementation of
ostringstream on system that does not support it:
class ostringstream : public ostrstream {
public:
string str() {
char *cstr = ostrstream::str ();
freeze(0);
if (cstr == 0) return string();
return string(cstr,pco unt());
}
};
I have two options:
- Enhance the limited implementation, which I am not very keen on
- Find a smarter way to do str("") that works for both ostringstream and
my limited ostringstream
Any advices ?
Thanks,
Mathieu
Ps: I tried s.seekp(0) instead of str("") but that leave garbage at the
end... 3 5609
Mathieu Malaterre wrote: The problem is that behind the scene I have a limited implementation
of ostringstream on system that does not support it:
class ostringstream : public ostrstream { public: string str() { char *cstr = ostrstream::str (); freeze(0); if (cstr == 0) return string(); return string(cstr,pco unt()); } };
Yuk! This code is not guaranteed to work: with the call to 'freeze(0)'
you state that you are done with the returned pointer and your are
not going to access it any further.You actually break this contract.
In addition, 'pcount()' is only supposed to return the number of
characters inserted by the last unformatted output operation (if I
remember correctly). If you insist in using 'std::ostrstrea m'
underneath, you should terminate the string properly (e.g. using the
manipulator 'std::ends') and then use an appropriate constructor of
'std::string'.
I have two options:
- Enhance the limited implementation, which I am not very keen on - Find a smarter way to do str("") that works for both ostringstream
and my limited ostringstream
I see at last two more options which should be considered:
- Ditch your current compiler and/or standard library and upgrade to a
reasonable one!
- Write your own stream class which just does the right thing: it just
takes a dozen of lines or so.
If all you need is a simple 'std::ostringst ream' replacement which may
be
restricted to fixed size buffer but supports 'str(std::strin g const&)',
this just takes a few lines of code, roughly something like this:
struct stringbuf:
std::streambuf
{
enum { s_size = 1024 };
stringbuf() { setp(m_buf, m_buf + s_size); }
std::string str() const { return std::string(pba se(), pptr()); }
void str(std::string const& s) {
std::copy(s.beg in(), s.end(), m_buf);
}
private:
char m_buf[s_size];
};
struct ostringstream:
private virtual stringbuf,
std::ostream
{
ostringstream() : std::ostream(st atic_cast<std:: streambuf*>(thi s))
{}
};
Well, in a real implementation I would use a more solid approach than
just deriving from the stream buffer but I'm too lazy to show it once
again (I have posted many examples of this in the past).
Any advices ?
My preference would be:
- Use a reasonable standard library.
- Use a appropriate stream buffer like the one above (although you
probably would add error checking and possibly lift the size
restriction).
- I would *not* use either of your options.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
Dietmar Kuehl wrote: Mathieu Malaterre wrote:
The problem is that behind the scene I have a limited implementation
of
ostringstre am on system that does not support it:
class ostringstream : public ostrstream { public: string str() { char *cstr = ostrstream::str (); freeze(0); if (cstr == 0) return string(); return string(cstr,pco unt()); } };
Yuk! This code is not guaranteed to work: with the call to 'freeze(0)' you state that you are done with the returned pointer and your are not going to access it any further.You actually break this contract. In addition, 'pcount()' is only supposed to return the number of characters inserted by the last unformatted output operation (if I remember correctly). If you insist in using 'std::ostrstrea m' underneath, you should terminate the string properly (e.g. using the manipulator 'std::ends') and then use an appropriate constructor of 'std::string'.
I have two options:
- Enhance the limited implementation, which I am not very keen on - Find a smarter way to do str("") that works for both ostringstream
and
my limited ostringstream
I see at last two more options which should be considered: - Ditch your current compiler and/or standard library and upgrade to a reasonable one! - Write your own stream class which just does the right thing: it just takes a dozen of lines or so.
If all you need is a simple 'std::ostringst ream' replacement which may be restricted to fixed size buffer but supports 'str(std::strin g const&)', this just takes a few lines of code, roughly something like this:
struct stringbuf: std::streambuf { enum { s_size = 1024 }; stringbuf() { setp(m_buf, m_buf + s_size); } std::string str() const { return std::string(pba se(), pptr()); } void str(std::string const& s) { std::copy(s.beg in(), s.end(), m_buf); } private: char m_buf[s_size]; };
struct ostringstream: private virtual stringbuf, std::ostream { ostringstream() : std::ostream(st atic_cast<std:: streambuf*>(thi s)) {} };
Well, in a real implementation I would use a more solid approach than just deriving from the stream buffer but I'm too lazy to show it once again (I have posted many examples of this in the past).
Any advices ?
My preference would be: - Use a reasonable standard library. - Use a appropriate stream buffer like the one above (although you probably would add error checking and possibly lift the size restriction). - I would *not* use either of your options. --
Dietmar,
Here is the patch I am using now: http://public.kitware.com/cgi-bin/vi...&r2=1.6&r1=1.5
I'll see tomorow if this works on the broken plateforms ITK is building on.
Thanks for your help
Mathieu
Mathieu Malaterre wrote: Here is the patch I am using now:
http://public.kitware.com/cgi-bin/vi...&r2=1.6&r1=1.5
This stuff uses some rather fishy techiques! I recommend
that you use the approach I have proposed: unless I have
made some blatant and obvious programming errors (I didn't
test the code prior to posting), it is the way stream
buffers are intended to be used. I don't have the standard
C++ library implementation I have done with me but I'm
pretty sure that the basics are the same. Of course, it is
your choice to ignore my advice on IOStreams but you might
want to briefly investigate my background before doing so
(e.g. have a look who translated/extended the IOStream
chapter of Nico's "The C++ Standard Library").
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Als |
last post by:
What's the meaning of "freeze" as member of ostringstream class? Is it
needed always to call freeze() at the disposal of an ostringstream object?
Is it also needed before disposing an istringsteam object?
Any comment is appreciated.
|
by: Charles Prince |
last post by:
How do I reuse a ostrstream?
So far I have replaced all code that does this
"delete <ostrstream>.str()"
with
"<ostrstream>.freeze(0)"
|
by: Simon Pryor |
last post by:
I am having some strange problems using std::ostringstream.
The simple stuff works okay, but trying to use:
std::ostringstream::str(const std::string&)
or:
std::ostringstream::ostringstream(const std::string&)
Gives some weird results on both Solaris & Linux.
Either that or I'm missing something. I've made a simple program to
|
by: becte |
last post by:
I encountered the following code similar to this
// some header files
static char* func(int i)
{
ostrstream out;
if (i==1) out << "ABCDE";
else if (i==2) out << "123";
else cout << "";
|
by: b83503104 |
last post by:
Hi,
An old code is using stuff like
#include <strstream.h>
ostrstream *m_actionStream;
and the compiler complained syntax error before `*' token
I followed some previous posts and tried things like
#include <strstream.h>
or
| |
by: tcl |
last post by:
Questions on ostrstream.
#1) do I have a memory leak as the control exits the scope
{
ostrstream os;
os << "hello world" << endl << ends;
}
|
by: GeeBee |
last post by:
1) I’m using Borland C++ Builder 1.0 (a very old (but still good) version).
2) I have an application EXE calling a DLL.
3) A function in the DLL receives a parameter defined as "ostream &" ,
and writes error messages to this stream using the “<<” operator.
4) The EXE file creates an ostrstream variable and passes this to the
DLL function.
5) After the DLL function has finished, the EXE writes some text to an
output file, followed by the...
|
by: Generic Usenet Account |
last post by:
With the deprecated ostrstream class, when the constructor is invoked
without arguments, memory is dynamically allocated. In that case the
onus on freeing the memory lies with the user. Typically this is done
by obtaining the char buffer (by invoking the str() method) and then
explicitly deleting it.
Does the ostringstream class also have the same issue? I mean, if I
instantiate ostringstream without any constructor arguments, is the...
|
by: Bala2508 |
last post by:
Hi,
I have a C++ application that extensively uses std::string and
std::ostringstream in somewhat similar manner as below
std::string msgHeader;
msgHeader = "<";
msgHeader += a;
msgHeader += "><";
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |