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

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

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 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 on other sites
std::ifstream file(nameFile,std::ios::binary);file.write(rawdata,blockMemorySize);

I don't see where you open the file in your code.

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 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 on other sites
Thank you Rip-off,
i appreciate it. I will try with your suggestion. If i get some good results, i will post them here.
Bye.

Share on other sites
Hi,
i have solved my problem using a :

class char_array_buffer : public std::streambuf

from website www(dot)mr-edd(dot)co(dot)uk/?p=16

Thanx.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×