Read memory buffer via stream interface

S

Steven Woody

Hi,

for example, i have

unsigned char buff[ 100 ]

the buffer hold a sequences of raw bytes. My question is, what is the
standard method of reading the `buff' via std::istream interface?

A relative question is: how to write to the `buff' via std::eek:stream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.

Thanks.

-
woody
 
K

kasthurirangan.balaji

Hi,

for example, i have

  unsigned char buff[ 100 ]

the buffer hold a sequences of raw bytes.  My question is, what is the
standard method of reading the `buff' via std::istream interface?

A relative question is: how to write to the `buff' via std::eek:stream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.

Thanks.

-
woody

Use std::istringstream for input, std::eek:stringstream for output,
std::stringstream for both. Once you have created a stringstream, you
can use istream/ostream read/write facilities on it.

#include <iostream>
#include <cstring>
#include <sstream>

main()
{
std::stringstream sstr;
sstr << "balaji";
std::cout << sstr.str() << '\n';
sstr.str(""); //clear in-memory data
char buf[100];
strcpy(buf,"asdkjabvdba");
std::stringstream sstr1(buf);
std::cout << sstr1.str() << '\n';
}

Thanks,
Balaji.
 
S

Steven Woody

for example, i have
unsigned char buff[ 100 ]
the buffer hold a sequences of raw bytes. My question is, what is the
standard method of reading the `buff' via std::istream interface?
A relative question is: how to write to the `buff' via std::eek:stream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.

-
woody

Use std::istringstream for input, std::eek:stringstream for output,
std::stringstream for both. Once you have created a stringstream, you
can use istream/ostream read/write facilities on it.

#include <iostream>
#include <cstring>
#include <sstream>

main()
{
std::stringstream sstr;
sstr << "balaji";
std::cout << sstr.str() << '\n';
sstr.str(""); //clear in-memory data
char buf[100];
strcpy(buf,"asdkjabvdba");
std::stringstream sstr1(buf);
std::cout << sstr1.str() << '\n';

}

Thanks,
Balaji.

Thanks a lot! I just did not see that a stringstream can be
constructed from a char[]. My book does not mentioned that. Where's
a good STL reference I can find online or free?

Another question I also like to ask here:

After,
char buf[100];
strcpy(buf,"123\n");
std::stringstream sstr1(buf);

How can I let cout << sstr1.str() << endl; prints something like that:

0x31 0x32 0x33 0x0a ?

Thanks again.

-
woody
 
K

kasthurirangan.balaji

Hi,
for example, i have
  unsigned char buff[ 100 ]
the buffer hold a sequences of raw bytes.  My question is, what is the
standard method of reading the `buff' via std::istream interface?
A relative question is: how to write to the `buff' via std::eek:stream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.
Thanks.
-
woody
Use std::istringstream for input, std::eek:stringstream for output,
std::stringstream for both. Once you have created a stringstream, you
can use istream/ostream read/write facilities on it.
#include <iostream>
#include <cstring>
#include <sstream>
main()
{
std::stringstream sstr;
sstr << "balaji";
std::cout << sstr.str() << '\n';
sstr.str(""); //clear in-memory data
char buf[100];
strcpy(buf,"asdkjabvdba");
std::stringstream sstr1(buf);
std::cout << sstr1.str() << '\n';

Thanks,
Balaji.

Thanks a lot!  I just did not see that a stringstream can be
constructed from a char[].  My book does not mentioned that.  Where's
a good STL reference I can find online or free?

Another question I also like to ask here:

After,
  char buf[100];
  strcpy(buf,"123\n");
  std::stringstream sstr1(buf);

How can I let cout << sstr1.str() << endl; prints something like that:

  0x31 0x32 0x33 0x0a ?

Thanks again.

-
woody- Hide quoted text -

- Show quoted text -

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) << ' ';
}

main()
{
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());
std::for_each(temp.begin(),temp.end(),print);
}


You may also try for ebooks of C++ primer and The C++ Standard Library
by Josuttis.

Thanks,
Balaji.
 
J

James Kanze

On Mar 5, 4:57 pm, Steven Woody <[email protected]> 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.

(It's also considered good practice to restore the flags of an
output stream after modifying them. You don't necessarily want
all of the output in the rest of the program to be in hex.)

And of course, you'll need to declare main to return an int.
{
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?
 
K

kasthurirangan.balaji

On Mar 5, 4:57 pm, Steven Woody <[email protected]> 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?
(It's also considered good practice to restore the flags of an
output stream after modifying them.  You don't necessarily want
all of the output in the rest of the program to be in hex.)
definitely yes, thought OP might include that as part of his
requirement.

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. By making const and reference(to a temporary), i
went ahead with the below. If wrong, pls correct.
--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thanks,
Balaji.
 
S

Steven Woody

On Mar 5, 5:38 pm, (e-mail address removed) wrote:
Hi,
for example, i have
unsigned char buff[ 100 ]
the buffer hold a sequences of raw bytes. My question is, what is the
standard method of reading the `buff' via std::istream interface?
A relative question is: how to write to the `buff' via std::eek:stream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.
Thanks.
-
woody
Use std::istringstream for input, std::eek:stringstream for output,
std::stringstream for both. Once you have created a stringstream, you
can use istream/ostream read/write facilities on it.
#include <iostream>
#include <cstring>
#include <sstream>
main()
{
std::stringstream sstr;
sstr << "balaji";
std::cout << sstr.str() << '\n';
sstr.str(""); //clear in-memory data
char buf[100];
strcpy(buf,"asdkjabvdba");
std::stringstream sstr1(buf);
std::cout << sstr1.str() << '\n';
}
Thanks,
Balaji.
Thanks a lot! I just did not see that a stringstream can be
constructed from a char[]. My book does not mentioned that. Where's
a good STL reference I can find online or free?
Another question I also like to ask here:
After,
char buf[100];
strcpy(buf,"123\n");
std::stringstream sstr1(buf);
How can I let cout << sstr1.str() << endl; prints something like that:
0x31 0x32 0x33 0x0a ?
Thanks again.
-
woody- Hide quoted text -
- Show quoted text -

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) << ' ';

}

main()
{
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());
std::for_each(temp.begin(),temp.end(),print);

}

You may also try for ebooks of C++ primer and The C++ Standard Library
by Josuttis.

Thanks,
Balaji.

Thank you Balaji!
 
M

Micah Cowan

Steven Woody said:
Thanks a lot! I just did not see that a stringstream can be
constructed from a char[]. My book does not mentioned that. Where's
a good STL reference I can find online or free?

That's because, technically speaking, it can't. It can, however, be
constructed from a std::string, which defines an implicit construction
from const char *, which your compiler will helpfully find and use in
the construction of a stringstream.

<strstream> (as opposed to <sstream>) defines std::strstream classes
that deal directly with const char *s; however, this is a deprecated
feature, which may be removed in the future.
 
S

Steven Woody

Steven Woody said:
Thanks a lot! I just did not see that a stringstream can be
constructed from a char[]. My book does not mentioned that. Where's
a good STL reference I can find online or free?

That's because, technically speaking, it can't. It can, however, be
constructed from a std::string, which defines an implicit construction
from const char *, which your compiler will helpfully find and use in
the construction of a stringstream.

Thanks for telling me that, i understood. But this lead me to believe
that Balaji is actually not what i wanted. Since, the constructed
stringstream merely operated on a copy of the `char buff[ 100 ]' not
on itself. So when I write to the streamstring, the buff[ 100 ] keeps
unchanged that is not my expection.

it seems the question is still open :(
 
K

kasthurirangan.balaji

Steven Woody said:
Thanks a lot!  I just did not see that a stringstream can be
constructed from a char[].  My book does not mentioned that.  Where's
a good STL reference I can find online or free?
That's because, technically speaking, it can't. It can, however, be
constructed from a std::string, which defines an implicit construction
from const char *, which your compiler will helpfully find and use in
the construction of a stringstream.

Thanks for telling me that, i understood.  But this lead me to believe
that Balaji is actually not what i wanted.  Since, the constructed
stringstream merely operated on a copy of the `char buff[ 100 ]' not
on itself. So when I write to the streamstring, the buff[ 100 ] keeps
unchanged that is not my expection.

it seems the question is still open :(




<strstream> (as opposed to <sstream>) defines std::strstream classes
that deal directly with const char *s; however, this is a deprecated
feature, which may be removed in the future.

- Show quoted text -

my understanding from the statement - How can I let cout <<
sstr1.str() << endl; prints something like that:


0x31 0x32 0x33 0x0a ?
was you just wanted to print in that fashion. With what i understand
now, you may want to use
std::transform(temp.begin(),temp.end(),&buf[0],convert())
where your convert is

#include <functional>
struct convert : public std::unary_function<T,V>
{
V operator()(const T &c)
{
//return hex value from a map
//have your own hex conversion alogrithm for storage
//any std algorithm which i am unaware to convert to hex
and store
}
};

ideally T would be char and V could very well be char or int based on
your design. Note that, print formatting is required. Also, i assume
that space during print is for print only. If not, you may need to
design your transform better.

Thanks,
Balaji.
 
S

Steven Woody

Thanks a lot! I just did not see that a stringstream can be
constructed from a char[]. My book does not mentioned that. Where's
a good STL reference I can find online or free?
That's because, technically speaking, it can't. It can, however, be
constructed from a std::string, which defines an implicit construction
from const char *, which your compiler will helpfully find and use in
the construction of a stringstream.
Thanks for telling me that, i understood. But this lead me to believe
that Balaji is actually not what i wanted. Since, the constructed
stringstream merely operated on a copy of the `char buff[ 100 ]' not
on itself. So when I write to the streamstring, the buff[ 100 ] keeps
unchanged that is not my expection.
it seems the question is still open :(
- Show quoted text -

my understanding from the statement - How can I let cout <<
sstr1.str() << endl; prints something like that:

0x31 0x32 0x33 0x0a ?
was you just wanted to print in that fashion. With what i understand
now, you may want to use
std::transform(temp.begin(),temp.end(),&buf[0],convert())
where your convert is

#include <functional>
struct convert : public std::unary_function<T,V>
{
V operator()(const T &c)
{
//return hex value from a map
//have your own hex conversion alogrithm for storage
//any std algorithm which i am unaware to convert to hex
and store
}

};

ideally T would be char and V could very well be char or int based on
your design. Note that, print formatting is required. Also, i assume
that space during print is for print only. If not, you may need to
design your transform better.

Thanks,
Balaji.

Thank you, but the code above only address formating problem. My
primary concert is how to operate the buff[ 100 ] via stream
interface. your implemention merely operate on the copy of the
buff[], not the buff[] itself. I may change the thread title as
"operate memory buffer ... " instead of "read memory buffer ..." to
refresh my real concern. Is there really no such a solution in STL?


Thanks.
 
K

kasthurirangan.balaji

Thanks a lot!  I just did not see that a stringstream can be
constructed from a char[].  My book does not mentioned that.  Where's
a good STL reference I can find online or free?
That's because, technically speaking, it can't. It can, however, be
constructed from a std::string, which defines an implicit construction
from const char *, which your compiler will helpfully find and use in
the construction of a stringstream.
Thanks for telling me that, i understood.  But this lead me to believe
that Balaji is actually not what i wanted.  Since, the constructed
stringstream merely operated on a copy of the `char buff[ 100 ]' not
on itself. So when I write to the streamstring, the buff[ 100 ] keeps
unchanged that is not my expection.
it seems the question is still open :(
<strstream> (as opposed to <sstream>) defines std::strstream classes
that deal directly with const char *s; however, this is a deprecated
feature, which may be removed in the future.
my understanding from the statement - How can I let cout <<
sstr1.str() << endl; prints something like that:
  0x31 0x32 0x33 0x0a ?
 was you just wanted to print in that fashion. With what i understand
now, you may want to use
std::transform(temp.begin(),temp.end(),&buf[0],convert())
where your convert is
#include <functional>
struct convert : public std::unary_function<T,V>
{
        V operator()(const T &c)
        {
           //return hex value from a map
           //have your own hex conversion alogrithm for storage
           //any std algorithm which i am unaware to convert to hex
and store
         }

ideally T would be char and V could very well be char or int based on
your design. Note that, print formatting is required. Also, i assume
that space during print is for print only. If not, you may need to
design your transform better.
Thanks,
Balaji.

Thank you, but the code above only address formating problem. My
primary concert is how to operate the buff[ 100 ] via stream
interface.  your implemention merely operate on the copy of the
buff[], not the buff[] itself. I may change the thread title as
"operate memory buffer ... " instead of "read memory buffer ..." to
refresh my real concern. Is there really no such a solution in STL?

Thanks.- Hide quoted text -

- Show quoted text -
transform writes back to the original buf, but the source being a
temporary. i see you want to directly access the memory as streams
without a copy. to my knowledge, i do not think we have one, googling
might help. another solution is rolling out your own - the hardest. i
am sorry i am unable to help you further in this regard.

Thanks,
Balaji.
 
J

James Kanze

On Mar 5, 2:12 pm, (e-mail address removed) wrote:
On Mar 5, 4:57 pm, Steven Woody <[email protected]> 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.)

[...]
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.

You need the variable, but there's no reason for it to be a
reference.
 
K

kasthurirangan.balaji

On Mar 5, 2:12 pm, (e-mail address removed) 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.

You need the variable, but there's no reason for it to be a
reference.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Merci beaucoup James.

Thanks,
Balaji.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top