Re: Read memory buffer via stream interface
On Mar 5, 3:17 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 6:49 pm, James Kanze <james.ka...@gmail.com> wrote:
On Mar 5, 2:12 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 4:57 pm, Steven Woody <narkewo...@gmail.com> wrote:
[...]
You may want to use this.
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <string>
void print(const char &c)
{
std::cout << std::showbase << std::hex << toascii(c) << ' ';
What's "toascii"? The only "toascii" I know of is a very old
pre-standard C Unix function---from the days when isupper, etc.
only worked for ASCII characters.
yes, i know. may i know whats its replacement?
Nothing.
As far as I know, it only existed on very early Unix
implementations, where char was signed, and the isxxx functions
only worked with input in the range 0x00-0x7F. And even there,
I'm not sure what it did if the input wasn't in this range: the
usual idiom for testing was:
if ( isascii( ch ) && isupper( ch ) ) ...
or something alont those lines.
The isascii function was dropped when the requirement that the
isxxx functions work on the range 0...UCHAR_MAX was introduced.
I'm not sure, but I don't think that toascii() was even
considered then, since it wasn't that widespread or that widely
used.
If the goal is to output the encoding of a character, the usual
procedure would be:
std::cout << (int)(unsigned char)ch ;
(Real masochists use static_cast instead of the C style
casts:-).) There's nothing to prevent you from providing
something like:
inline int
isAscii( char ch )
{
return (int)(unsigned char)ch ;
}
, but in my experience, the necessity doesn't occur frequently
enough to justify it. (And of course, the name isn't really
correct, since what it will return isn't guaranteed to be
ASCII.)
[...]
}
main()
And of course, you'll need to declare main to return an int.
agreed.
{
std::stringstream sstr;
sstr << "balaji";
std::cout << sstr.str() << '\n';
sstr.str(""); //clear data
char buf[100];
strcpy(buf,"123\n");
std::stringstream sstr1(buf);
std::cout << sstr1.str() << '\n';
const std::string &temp(sstr1.str());
Why the reference here?
i thought of using sstr1.str().begin() - which i am not sure
whether it would work or not.
The function itself will work, but I wouldn't recommend it; the
iterator it returns will cease to be valid at the end of the
full expression:-). And of course, do the same for end(), and
the two iterators will be into two different temporary objects.
My question was more along the lines of: why not:
std::string temp( sstr1.str() ) ;
(Any considerations as to the number of copies would be
premature, besides which, you'll get a copy in both cases
anyway.)
By making const and reference(to a temporary), i went ahead
with the below. If wrong, pls correct.
std::for_each(temp.begin(),temp.end(),print);
You need the variable, but there's no reason for it to be a
reference.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34