CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2003
    Posts
    188

    handling binary streams?

    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?

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449
    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:ata().

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Would you recommend using std::string for this purpose? I am
    having a little trouble getting my head around streams and
    memory buffers
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    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

  6. #6
    Join Date
    Jun 2003
    Posts
    188
    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:ata().

    Regards,

    Paul McKenzie

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

  7. #7
    Join Date
    Dec 2002
    Posts
    47
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured