Click to See Complete Forum and Search --> : handling binary streams?


mburke
October 10th, 2003, 12:53 PM
I am building a web server. And some of the data that will be tramsitted back to the browser are images. From what I understand, images have embedded zeros in them.

Can I use the string class to handle it? Or do I need some kind of byte stream? Or a simple char array?

mwilliamson
October 10th, 2003, 01:17 PM
You can use a string (actually only a char array), but you can't treat it as a string. You will have to store the length, and you can't use string functions like strcpy instead use memcpy.

Paul McKenzie
October 10th, 2003, 02:36 PM
You can use std::string to hold binary data, including embedded nulls. The member functions to use to store data in a std::string is std::string::assign().

To return a pointer to the binary data, the member function to use is std::string::data().

Regards,

Paul McKenzie

souldog
October 10th, 2003, 03:35 PM
Would you recommend using std::string for this purpose? I am
having a little trouble getting my head around streams and
memory buffers

Paul McKenzie
October 10th, 2003, 03:57 PM
Originally posted by souldog
Would you recommend using std::string for this purpose? I am
having a little trouble getting my head around streams and
memory buffers I've stored binary data in std::string's (images and binary file data), and have had no problems.

Regards,

Paul McKenzie

mburke
October 10th, 2003, 06:42 PM
Originally posted by Paul McKenzie
You can use std::string to hold binary data, including embedded nulls. The member functions to use to store data in a std::string is std::string::assign().

To return a pointer to the binary data, the member function to use is std::string::data().

Regards,

Paul McKenzie


Great, I was hoping I could use std::string. Thank you

rfmobile
October 12th, 2003, 06:08 AM
You can also use std::vector<char> or std::vector<unsigned char>. It's got most of the same methods as std::string but does not try to append a nul to the end.

-rick