473,325 Members | 2,828 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,325 software developers and data experts.

ostringstream and sgetn() weirdness

hi,

i have come aross a strange problem with a bit of code that uses a
ostringstream to build up a string and i extract the string into a
user buf via the sgetn() call instead of via the str() method.
however, it appears that when the contents extracted via sgetn() are
invalid for the first extract.

below is a test prog that demonstrates the issue. we are using Forte
7.0 on solaris 5.8; i dont have access to gcc (or any other c++
compiler). can someone tell me where i'm going wrong?
thanks
ray
#include <strings.h>

#include <iostream>
#include <sstream>
#include <exception>
#include <string>

using namespace std;
int main(int argc, char* argv[])
{
try
{
ostringstream dump;
size_t sz = 0;

if (argc == 1) {
dump << "huh?";
sz = dump.tellp();
char tmp[5];
memset(tmp, 0, 5);
dump.rdbuf()->sgetn(tmp, 4);
tmp[4] = NULL;
dump.seekp(0);

// this comes out as empty string!!!
cout << "init str='" << tmp << "'" << endl;
}
const char* what = "abcdefghijklmnopqrstuvwxyz";
dump << what;
sz = dump.tellp();

char* s = new char[sz+1];
memset(s, 0, sz+1);
dump.rdbuf()->sgetn(s, sz);
s[sz] = NULL;
dump.seekp(0);

cout << "inserted string is ok: " << (strcmp(what, s) ==
0 ? "yes" : "no") << endl;
what = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
dump << what;
sz = dump.tellp();
memset(s, 0, sz+1);
dump.rdbuf()->sgetn(s, sz);
s[sz] = NULL;
cout << "inserted shorter string is ok: " << (strcmp(what,
s) == 0 ? "yes" : "no") << endl;

delete [] s;
}
catch (const exception& ex)
{
cerr << ex.what() << endl;
}

return 0;
}

Jul 2 '07 #1
3 3568

<wh************@yahoo.co.ukwrote in message...
hi,
i have come aross a strange problem with a bit of code that uses a
ostringstream to build up a string and i extract the string into a
user buf via the sgetn() call instead of via the str() method.
however, it appears that when the contents extracted via sgetn() are
invalid for the first extract.

below is a test prog that demonstrates the issue. we are using Forte
7.0 on solaris 5.8; i dont have access to gcc (or any other c++
compiler). can someone tell me where i'm going wrong?
thanks
ray

#include <strings.h>
#include <iostream>
#include <sstream>
#include <exception>
#include <string>
using namespace std;

int main(int argc, char* argv[]){
try{
ostringstream dump;
Note 'dump' is an *output* stringstream.
size_t sz = 0;

if (argc == 1) {
dump << "huh?";
sz = dump.tellp();
char tmp[5];
memset(tmp, 0, 5);
dump.rdbuf()->sgetn(tmp, 4);
Now you want to read it in? ( dump.seekg(0); ? ).
tmp[4] = NULL;
dump.seekp(0);

// this comes out as empty string!!!
cout << "init str='" << tmp << "'" << endl;
}
int main(int argc, char* argv[]){
// try{
// std::ostringstream dump;
std::stringstream dump;
size_t sz = 0;

if (argc == 1) {
dump << "huh?";
sz = dump.tellp();
char tmp[5];
memset(tmp, 0, 5);

dump.seekg(0); // for illustration. try with ostream.
dump.rdbuf()->sgetn(tmp, 4);

std::istringstream iss( dump.str() );
std::string temp( 4, ' ' ); // or: std::vector<chartemp(4).
iss.read( &temp.at(0), 4 );

// tmp[4] = NULL; // assignment to non-pointer type 'char'
tmp[4] = '\0';
dump.seekp(0);

// this comes out as empty string!!!
cout << "init str='" << tmp << "'" <<std::endl;
cout << "temp str='" << temp << "'" <<std::endl;
} // if(argc)
// .......
} // main()
/* -output-
init str='huh?'
temp str='huh?'
*/

--
Bob R
POVrookie
Jul 2 '07 #2
On Jul 2, 7:21 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
ostringstream dump;

Note 'dump' is an *output* stringstream.
size_t sz = 0;
if (argc == 1) {
dump << "huh?";
sz = dump.tellp();
char tmp[5];
memset(tmp, 0, 5);
dump.rdbuf()->sgetn(tmp, 4);

Now you want to read it in? ( dump.seekg(0); ? ).
tmp[4] = NULL;
dump.seekp(0);
// this comes out as empty string!!!
cout << "init str='" << tmp << "'" << endl;
}
it is **intended** to be an OUTput stream so i see no reason why it
would fail

i am simply extracting the data that has been inserted into the
stream, otherwise i would have to do call dump.str() to get me the
data which is inexpensive -- the potential copy of the data and the
construction of a string that is then copied as its passed back to
me.. remember the return type for stringstream::str() is string (not a
ref, but a copy)

changing it to a stringstream works the way i intended but wondered
why my example didnt

Jul 3 '07 #3

<wh************@yahoo.co.ukwrote in message...
On Jul 2, 7:21 pm, "BobR" wrote:
ostringstream dump;
Note 'dump' is an *output* stringstream.
size_t sz = 0;
if (argc == 1) {
dump << "huh?";
sz = dump.tellp();
char tmp[5];
memset(tmp, 0, 5);
dump.rdbuf()->sgetn(tmp, 4);
Now you want to read it in? ( dump.seekg(0); ? ).
Note that line!
tmp[4] = NULL;
dump.seekp(0);
// this comes out as empty string!!!
cout << "init str='" << tmp << "'" << endl;
}

it is **intended** to be an OUTput stream so i see no reason why it
would fail

i am simply extracting the data that has been inserted into the
stream, otherwise i would have to do call dump.str() to get me the
data which is inexpensive
Then why worry about it?
-- the potential copy of the data and the
construction of a string that is then copied as its passed back to
me.. remember the return type for stringstream::str() is string (not a
ref, but a copy)

changing it to a stringstream works the way i intended but wondered
why my example didnt
Did you miss the little notes I made on '.seekg()'? That is the 'get
pointer', and an ostream does not have one.

std::ostringstream dump;
cout<<"dump.tellg()='"<<dump.tellg()<<std::endl;
// error: `tellg' undeclared (first use this function)

dump.seekg(0);
// error: `seekg' undeclared (first use this function)

So, it seems 'sgetn()' needs the 'get pointer' (even has 'get' in it's
name!).
Can you see that now?

--
Bob R
POVrookie
Jul 3 '07 #4

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

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
5
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:...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
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...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
3
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...
0
by: whatdoineed2do | last post by:
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...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
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
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
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.