This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: stringstream vs. strstream


>>>>> "Jimen" == Jimen Ching <Ching> writes:

    Jimen> Hi all,
    Jimen> Given this source code:
    Jimen> ----------------------------------------------------------------------
    Jimen> #include <iostream>
    Jimen> #include <sstream>
    Jimen> #include <strstream>
    Jimen> #include <string>

    Jimen> int
    Jimen> main()
    Jimen> 	{
    Jimen> 	stringstream s1;
    Jimen> 	strstream s2;
    Jimen> 	string s, fmt("string");

    Jimen> 	s1 << fmt[2];
    Jimen> 	s1 << ends;
    Jimen> 	s = s1.str();
    Jimen> 	cout << "s1: " << s << endl;
    Jimen> 	cout << "s1 size: " << s.size() << endl;

    Jimen> 	s2 << fmt[1];
    Jimen> 	s2 << ends;
    Jimen> 	s = s2.str();
    Jimen> 	cout << "s2: " << s << endl;
    Jimen> 	cout << "s2 size: " << s.size() << endl;

    Jimen> 	return 0;
    Jimen> 	}
    Jimen> ----------------------------------------------------------------

    Jimen> The following output is generated:
    Jimen> $ uname -a
    Jimen> CYGWIN_NT-4.0 A19990524 1.3.9(0.51/3/2) 2002-01-21 12:48 i686 unknown
    Jimen> $ g++ -v
    Jimen> Reading specs from /bin/../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs
    Jimen> gcc version 2.95.3-5 (cygwin special)
    Jimen> $ g++ tst.cc
    Jimen> $ ./a.exe
    Jimen> s1: r^@
    Jimen> s1 size: 2
    Jimen> s2: t
    Jimen> s2 size: 1

    Jimen> Note, the shell used is bash.  So the '^@' is what is printed on the
    Jimen> console.

    Jimen> My question is, why is stringstream and strstream behaving differently?
    Jimen> I think stringstream should behave like strstream.  Comments?

They behave differently because they are different.  The newer "stringstream"
class provides the capability to directly insert null characters, which the
"ends" manipulator does.  When you use "stringstream", don't use "ends" to
null-terminate a string.  It's not necessary.

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]