473,378 Members | 1,355 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,378 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 1711
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 one tried to do this. Any help with buffer...
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 iostream constructs so that I need not have to define...
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 MyTextOut << "x = " << iX << " kg" ; and...
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 settle for a platform specific way of doing it. ...
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 still think I don't quite grasp what these settings...
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 help me: So far we allocated a block of memory as...
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, sizeof (a)); v.push_back(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 that tablequeue buffers are overflowing on a...
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 freed after calling,i always worry about memory...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.