472,353 Members | 1,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

formatting buffers

I want to get rid of my "sprintf" call, so I wrote the following
example, in which I have two problems:
(1) why do I need to inherit from "streambuf", and not just use it as a
local in my "ToBuff" function (I get a protected constructor error)?
(2) I fail to get the output of the formatting in my C buffer, the
output of the program is:
string is: 100
buffer is: hello world
When I hoped to get:
string is: 100
buffer is: 100

note: the "ToString" function is not a real option where large buffers
should be allocated outside the class in "C style" code

the code:
///////////////////////////////////////////////////////////
#include <ostream>
#include <sstream>
#include <stdio.h>
#include <iostream>
using namespace std;

template <typename T>
class A : public streambuf
{
public:
A(const T& t) : m_value(t) {}

char* ToBuff(char* szBuff, int nSize)
{
pubsetbuf(szBuff, static_cast<streamsize>(nSize));
ostream os(this);
os << m_value << ends;
return szBuff;
}

string ToString() const
{
ostringstream ost(ostringstream::out);
ost << m_value << ends;
return ost.str();
}

private:
T m_value;
};

int main()
{
char buff[32]="hello world";

A<double> a(99.99999);

cout << "string is: " << a.ToString() << endl;
printf("buffer is: %s\n", a.ToBuff(buff,32));

return 0;
}

Nov 22 '05 #1
3 1667
yuvalif wrote:
I want to get rid of my "sprintf" call, so I wrote the following
example, in which I have two problems:
(1) why do I need to inherit from "streambuf", and not just use it as a
local in my "ToBuff" function (I get a protected constructor error)?
(2) I fail to get the output of the formatting in my C buffer, the
output of the program is:
string is: 100
buffer is: hello world
When I hoped to get:
string is: 100
buffer is: 100

note: the "ToString" function is not a real option where large buffers
should be allocated outside the class in "C style" code

the code:
///////////////////////////////////////////////////////////
#include <ostream>
#include <sstream>
#include <stdio.h>
#include <iostream>
using namespace std;

template <typename T>
class A : public streambuf
{
public:
A(const T& t) : m_value(t) {}

char* ToBuff(char* szBuff, int nSize)
{
pubsetbuf(szBuff, static_cast<streamsize>(nSize));
ostream os(this);
os << m_value << ends;
return szBuff;
}

string ToString() const
{
ostringstream ost(ostringstream::out);
This is more simply written

ostringstream ost;
ost << m_value << ends;
No need for ends with streamstream. In fact it is incorrect, unless you
really do want a nul byte included in your C++ string.

ost << m_value;
return ost.str();
}

private:
T m_value;
};

int main()
{
char buff[32]="hello world";

A<double> a(99.99999);

cout << "string is: " << a.ToString() << endl;
printf("buffer is: %s\n", a.ToBuff(buff,32));

return 0;
}


Basically you are misusing streambuf. It's designed as a base for stream
buffers, not for converting values to strings. The class you do need is
ostrstream.

#include <strstream>

char* ToBuff(char* szBuff, int nSize)
{
ostrstream ost(szBuff, nSize);
ost << m_value << ends;
return szBuff;
}

Don't much like your class design either, I would make these two
template functions.

template <class T>
string toString(const T& x)
{
...
}

template <class T>
char* toBuffer(const T& x, char* buf, int size)
{
...
}

Seems easier to use to me.

All code untested.

john
Nov 22 '05 #2
thanks john, working fine.

Two questions:
(1) My compiler (gcc4.0) warn me that: <strstream> is depreciated, any
idea for another include?
(2) Regarding the design, if I already have a template class, why
shouldn't it have a member function for the conversion?

Yuval.

Nov 22 '05 #3
yuvalif wrote:
thanks john, working fine.

Two questions:
(1) My compiler (gcc4.0) warn me that: <strstream> is depreciated, any
idea for another include?
It is deprecated, but personally I wouldn't worry. It still has
legitimate uses. But if you really want to avoid it then I would try
something like this

char* ToBuff(char* szBuff, int nSize)
{
ToString().copy(szBuff, nSize);
return szBuff;
}

In fact maybe that is preferable anyway.

(2) Regarding the design, if I already have a template class, why
shouldn't it have a member function for the conversion?
I was suggesting that you replace the template class with two template
functions. But if you have the template class already for other reasons
then fine.

Yuval.


john
Nov 22 '05 #4

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

Similar topics

3
by: doodle4 | last post by:
Hello, I need to create 6 buffers in python and keep track of it. I need to pass this buffer, say buffer 1 as an index to a test app. Has any...
3
by: Matt | last post by:
Hello, Is there a means to use printf()-like formatting (eg: http://www.mkssoftware.com/docs/man1/printf.1.asp ) using class string and/or...
7
by: CSpartan | last post by:
I need to create a C++ function that works similar to 'cout'. I would like to have a function 'MyTextOut' that would accept the following syntax ...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I...
2
by: | last post by:
Hi, we are planning to rewrite an extisting C++ image processing application/library in C#. Now several question arouse where I hope you can...
2
by: Alex Vinokur | last post by:
I need a container of char-buffers, for instance, vector of char-buffers. vector<string> v; // It is not what I need to char a; memset (&a,...
0
by: Sam Durai | last post by:
Hello, A particular select query took unusually long time to execute hence I took an app.snap to find out what happens internally and I found out...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.