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

    How to construct a ifstream object from a buffer

    i'm using an image processing library to do my job.
    The libray loads images from file path or ifstream. But some pre-process must be done on the images.
    When pre-process is done, the only data i can provide for the library is a char buffer keeping the image(in the BMP format, including BMP info header, palette, and pixels).
    Is there any eazy way to construct a ifstream object from char buffer, cuz i cant modify the library.

    thanks
    Last edited by cuteyaoming; August 25th, 2005 at 08:24 AM.

  2. #2

    Lightbulb Re: How to construct a ifstream object from a buffer

    Quote Originally Posted by cuteyaoming
    Is there any eazy way to construct a ifstream object from char buffer, cuz i cant modify the library.
    Well you cannot convert char buffer to an ifstream. However, you can write the char buffer to the ifstream

    ----------------------

    Programming ( Assignment / Project ) Help

  3. #3
    Join Date
    May 2004
    Posts
    3

    Re: How to construct a ifstream object from a buffer

    Thanks a lot for helping me out.
    I've tried to work out a way like this. But as the ifstream doesn't provide a "write" method, how can i do this?
    Could you be kind to show me some fragments of code?

  4. #4
    Join Date
    Jan 2001
    Posts
    588

    Re: How to construct a ifstream object from a buffer

    Maybe you could just dump the contents of the buffer to a temporary file for loading by the library. You can't write data to an ifstream, as it is for input only.

  5. #5
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: How to construct a ifstream object from a buffer

    Use an istringstream and initialize it to a string containing the contents of your buffer. For example:

    Code:
    #include <sstream>
    ...
    char buffer[4096];
    // fill the buffer, null terminate it
    std::string strbuf = buffer;
    std::istringstream is(buffer);
    // now use 'is' as an istream object
    int number; string str;
    is >> number >> str;  // etc.
    // you can also pass it to a function which expects an istream& argument:
    some_function(is);
    // assuming some function's signature is some_function(istream&)
    Old Unix programmers never die, they just mv to /dev/null

  6. #6
    Join Date
    May 2004
    Posts
    3

    Re: How to construct a ifstream object from a buffer

    I appreciate this code fragment.
    But as i said, the buffer is actually an BMP image. What if some character in the buffer happens to be 0x00. Will istringstream truncate the buffer?
    Maybe istringstream is not quite safe in my situation. So what's your opinion.

    Thanks anyway.

  7. #7
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: How to construct a ifstream object from a buffer

    Istringstream will not truncate the buffer, this line will:

    Code:
    std::string strbuf = buffer;
    Instead, initialize the string like this:

    Code:
    std::string strbuf(buffer, buffer + 4096);
    This way, 4096 bytes will be copied into the string no matter what data they hold.

    EDIT: I checked the implementation of istringstream's constructor: no truncation happens, the full contents of the string are copied into the buffer regardless of whether the string contains a null.
    Last edited by HighCommander4; August 26th, 2005 at 04:01 PM.
    Old Unix programmers never die, they just mv to /dev/null

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