473,320 Members | 2,073 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: rdbuf()->sgetn(buf, rdbuf()->in_avail()) contentsdiffer from str()

hi,

i'm trying avoid uncessary copy of data whilst using the stringstream
so i am using the sgetn() to copy the data into a buf as apposed to
getting the same data via the .str() which will construct a string
object, and a string object copy on return to me.

however i've noticed that if the last thing put onto the stringstream
is not txt (an int for example) then the sgetn() using the in_avail()
DIFFERS from what i get back from .str(). Below illustrates my
problem

Am using SunStudio 10 on sparc 5.10 -- haven't had chance to try g++

any thoughts on where i may be going wrong appreciated
thanks
ray
#include <unistd.h>

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
stringstream s;

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
cout << "'" << s.str() << "'" << endl; // PID PRINTED AS EXPECTED
char buf[100];
memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // PID NOT PRINTED

memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid() << " and some text";
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // AS EXPECTED

return 0;
Feb 11 '08 #1
0 1654

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...
4
by: David Johnstone | last post by:
Hi Group, I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2). I have a situation where I need to tie an ostringstream to an istringstream so I can write to one and read back what I wrote...
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
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...
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 ;...
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
10
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
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");
2
by: jabbah | last post by:
hi I find myself using stringstream all over my code just to build debug messages that I may or may not write to cout later. that is very comfortable while developing but is generally useless for...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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
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...

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.