ostringstream and sgetn() weirdness

W

whatdoineed2do

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;
}
 
B

BobR

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::eek:stringstream 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<char> temp(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?'
*/
 
W

whatdoineed2do

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
 
B

BobR

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::eek:stringstream 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top