473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Problem with stringbuf

Sam
Andre Paim writes:
The following program:

#include <iostream>
#include <iterator>
#include <istream>
#include <sstream>

int main()
{
char buffer[] = {0x01, 0x0C, 0x1B};
std::stringbuf str_buf;
str_buf.pubsetbuf(buffer, sizeof (buffer));
std::istream iss(&str_buf);
std::cout.setf( std::ios::hex, std::ios::basefield );
std::copy(std::istream_iterator<char>(iss),
std::istream_iterator<char>(), std::ostream_iterator<unsigned
short>(std::cout, " "));
return (0);

Generates the output (gcc 4.2 on Linux):

1 1b

Why it does not print the 0x0C value?
Becase std::istream_iterator<chardoes not mean "return one character at a
time from a std::istream". It means "return the next element from a
std::istream, where each element is a single character, and all elements
are separated by whitespace characters".

It just so happens that 0x0c is the ascii formfeed character, which is a
whitespace character, to it is interpreted as a delimitor between the 0x01
and the 0x1b character.

Note that your std::ostream_iterator<unsigned shortproduces, on output,
whitespace-delimited integers. If you take this output, and supply it, as a
stream, to a suitably-configured std::istream_iterator<unsigned short>
(ignoring, for the moment the issue of hexadecimal vs decimal>, you will end
up iterating back over the same integers, with the iterator automatically
skipping the space characters that delimit your integers. Replace "integers"
with "chars", and that's what's happening with your input iterator.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkimChMACgkQx9p3GYHlUOL30gCfSitAztWAZD zMkdedaaxNAOcc
mrEAnRihiP/0lgEHsltyyQs3tHgBmSPs
=du9A
-----END PGP SIGNATURE-----

Aug 15 '08 #1
0 1427

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

Similar topics

1
8535
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
0
3053
by: FreeStyle | last post by:
Hi everybody, I'm using phpMyAdmin 2.2.6 with EasyPHP v.1.6.0.0 I wrote a php script which uses a username (added in the mysql base with phpMyAdmin). This user has no privileges. But in the...
3
6558
by: Marcus | last post by:
I'm really confused with something I never had any problems with before... I have a form with a textbox called password. On the second page that $password is posted to, if I test isset($password)...
2
4232
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
3
2018
by: David Chandler | last post by:
Let me try to describe the situation as clearly as I can. In namespace XXX I have a class PARENT with the following public functions: virtual void foo( const char* stringBuf ); // set...
0
1744
by: Steven T. Hatton | last post by:
I copied what I believe is a complete representation of <iosfwd> from ISO/IEC 14882:2003(E). My understanding of the rules covering typedefs, templates and forward declarations is not very solid. ...
0
1151
by: pjks | last post by:
Does anyone know how to assign a char * into a variable of type stringbuf without the stringbuf making a copy of what is pointed to by the char * Reason: If the char array pointed to by the...
10
3318
by: hsmit.home | last post by:
Hi everyone, I'm having some difficulty with the following piece of code. I have stripped it to it's bare minimum to demonstrate the problem at hand. Compiler: MS Visual C++ 2005 Express...
0
1278
by: marco.minerva | last post by:
Hi all! I'm using the std::stringbuf object. I insert a string in it using the "sputn" method, but how I can I remove, for example, the first 3 characters from the buffer? Thanks in advance....
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.