473,499 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read memory buffer via stream interface

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::ostream
interface? I think this can be done by using std::stringstream, but i
am not sure of it.

Thanks.

-
woody
Mar 5 '08 #1
6 5877
On Mar 5, 2:16*pm, Steven Woody <narkewo...@gmail.comwrote:
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::ostream
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::ostringstream 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.
Mar 5 '08 #2
On Mar 5, 5:38 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 2:16 pm, Steven Woody <narkewo...@gmail.comwrote:
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::ostream
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::ostringstream 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
Mar 5 '08 #3
On Mar 5, 4:57*pm, Steven Woody <narkewo...@gmail.comwrote:
On Mar 5, 5:38 pm, kasthurirangan.bal...@gmail.com wrote:


On Mar 5, 2:16 pm, Steven Woody <narkewo...@gmail.comwrote:
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::ostream
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::ostringstream 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.
Mar 5 '08 #4
On Mar 5, 2:12 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 4:57 pm, Steven Woody <narkewo...@gmail.comwrote:
[...]
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.)
}
main()
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?
std::for_each(temp.begin(),temp.end(),print);
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Mar 5 '08 #5
On Mar 5, 6:49*pm, James Kanze <james.ka...@gmail.comwrote:
On Mar 5, 2:12 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 4:57 pm, Steven Woody <narkewo...@gmail.comwrote:

* * [...]
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.
}
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. 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);
}

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
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.
Mar 5 '08 #6
On Mar 5, 9:12 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 4:57 pm, Steven Woody <narkewo...@gmail.comwrote:
On Mar 5, 5:38 pm, kasthurirangan.bal...@gmail.com wrote:
On Mar 5, 2:16 pm, Steven Woody <narkewo...@gmail.comwrote:
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::ostream
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::ostringstream 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!
Mar 5 '08 #7

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

Similar topics

5
17530
by: Sandy | last post by:
Hi, In one of my interview i was asked the difference between Stream and Buffer. can anybody explain the difference. Thanks
6
7341
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
74
4587
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
4
14649
by: ad | last post by:
Hi, I have a file (c:\myFile.zip) in the Server. How can I read the file into a Stream?
3
3367
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
21
6868
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
2
6599
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
70
1059
by: quickcur | last post by:
hi can anyone explain me to read image to memory from a url it is very easy in java but it is hard to find an complete solution in c/c++. Thanks,
3
11471
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
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
7223
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...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
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
5475
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
4919
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...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
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.