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

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 1418

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

Similar topics

1
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
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
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
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
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
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
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
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
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
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.