Thread: Binary data -> istream

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Binary data -> istream

    Assume I have a chunk of binary data in memory, I would like to turn it into an istream. How is this done? I've been thinking of using an std::stringstream first, but since the data is binary it's a bit risky I guess.

    Something along these lines:
    Code:
    void fun(char* data, int dataSize)
    {
       std::istream s(data, dataSize);
    
       stuff = s.get();
       s.read(&junk, sizeof(junk));
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Why do you need to convert the bytes into a stream? It's far simpler just to work with them directly, or write your own wrapper to make sure the buffer is worked with safely. If you really do need a stream, my first attempt would be to derive from basic_streambuf and then pass a pointer to an object of that derived class to istream's constructor.

    At the moment that's the only safe and portable way I can think of.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, the iostreams were never really intended for binary I/O. (On the other hand, we're not given an alternative...)

    Anyway, it's true that it's a little risky to use stringstreams. I don't know very much about this. I know that the C++ standard dictates that the strings works correctly even with embedded NUL characters. However, I do not know the requirements on char_traits when faced with weird characters. Neither do I know the requirements on basic_stringbuf.

    I agree with Sake. Writing your own streambuffer is probably the best choice. And make sure you post your code - something like that could be very useful. I know of at least one other person who requires what you do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The reason I need this is I have a generic loader function that uses an istream, then depending on where I want to load from (a file on the HD, a file located in memory) I pass different streams to it.

    I thought of writing something of my own too, but thought if there already exists a way it would be better...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Are the STL header files intentionally made totally unreadable, or are the creators just bad coders???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Are the STL header files intentionally made totally unreadable, or are the creators just bad coders???
    Templates were designed poorly. The result is a nearly impossible mass of dependencies to unravel.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Perhaps you could try a basic_istringstream<unsigned char>? According to the docs provided with MSVC Beta 2005, istringstream is just a typedef for basic_istringstream<char>. On the other hand, I'm not sure how char_traits<unsigned char> works..

    Code:
    template <
    classElem,
    classTr= char_traits<Elem>,
    classAlloc= allocator<Elem>
    >
    class basic_istringstream : public basic_istream<Elem,Tr>
    It'd be nice if you can find a way to fiddle around with that so that it behaves as you want. Unfortunately, I have no idea how any of the char_traits or allocator stuff works, so I can't be of any help there.

    **EDIT**
    [useless thought of the day]
    Or you could write the data to a file and open it with a binary ifstream
    [/useless thought of the day]
    Last edited by Hunter2; 01-23-2005 at 06:21 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>On the other hand, I'm not sure how char_traits<unsigned char> works..
    It doesn't. You would have to write it first because char_traits isn't specialized for unsigned char[*].

    >>Or you could write the data to a file and open it with a binary ifstream
    This is an option, though it would slow things down a bit because of the device I/O.

    [*] Unless the default char is unsigned for the implementation, but that's a dangerous assumption for this particular problem.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think MS's STL headers are intentionally unreadable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  3. Binary data handling
    By maverickbu in forum C Programming
    Replies: 1
    Last Post: 06-26-2007, 01:14 PM
  4. binary data file
    By tucker81 in forum C Programming
    Replies: 8
    Last Post: 06-08-2006, 12:50 AM
  5. Binary Tree Revisited: Near Completion
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2003, 08:23 AM