Sign in to follow this  

How to write a memory block (char*) into a ifstream

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi to all, i'm looking for writing a memory block char* into a ifstream. It seems that ifstream is strongly referred to a file on disk. I'm using this code : 1 _unformattedBinaryDataForImagefile* structData=0; 2 structData=_arrayForUnformattedBinaryData.at(imageIndexArray); 3 const char* nameFile=structData->_nameFile; 4 long blockMemorySize=structData->_memoryBlockSize; 5 char* rawdata=0; 6 rawdata=structData->_startPointer; 7 if (!rawdata) 8 { 9 // ERROR 10 } 11 else 12 { 13 std::ifstream unformattedDataFromImagefile; 14 unformattedDataFromImagefile.setf(ios::in|ios::out|ios::binary); 15 unformattedDataFromImagefile.rdbuf()->pubsetbuf(rawdata,blockMemorySize); 16 // DO SOMETHING 17 } I get data from memory at line 6 and memory size from line 4. I try to use ifstream from 13 to 15, but i get always a NULL data. Can anyone help me? Thank in advance.

Share this post


Link to post
Share on other sites
pubsetbuf
from cplusplus.com
Calls the protected virtual member setbuf, which is intended to set the array pointed by parameter s as the internal character sequece to be used by the stream buffer object, although specific implementations may vary.

If both parameters are zero, the object becomes unbuffered.


You are giving it memory to use as a buffer you are not writing a buffer out to the stream. ifstream is an input stream you use it to read from disk you can not write to disk with an ifstream you need an ofstream to write data. The function you are looking for is called write.

Share this post


Link to post
Share on other sites
Thank for yours replay.
I have not open ifstream, becouse i don't know how to open an ifstream with a block of memory instead of file on disk.
Furthermore, i have to use ifstream. I'm wondering if is it possible to fill ifstream with byte coming from memory (like char*) instead from file on disk?
Thanx.

Share this post


Link to post
Share on other sites
Oh, I misunderstood. I see what you are trying to do. You could consider std::stringstream if you don't mind the copying that will occur. Otherwise, you could write your own std::streambuf implementation, or find one online that does what you need. For example, boost provides an iostreams library. I haven't used it, but you might find something there.

Share this post


Link to post
Share on other sites

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this