Search code examples
c++visual-c++memorymemorystreamstreambuf

How can I read from memory just like from a file using wistream?


In my previous question I asked how to read from a memory just as from a file. Because my whole file was in memory I wanted to read it similarly.

I found answer to my question but actually I need to read lines as a wstring. With file I can do this:

wifstream file;
wstring line2;

file.open("C:\\Users\\Mariusz\\Desktop\\zasoby.txt");
if(file.is_open())
{
    while(file.good())
    {
        getline(file,line2);
        wcout << line2 << endl;
    }
}   
file.close();

Even if the file is in ASCII.

Right now I'm simply changing my string line to wstring with a function from this answer. However, I think if there is a way to treat this chunk of memory just like a wistream it would be a faster solution to get this lines as wstrings. And I need this to be fast.

So anybody know how to treat this chunk of memory as a wistream?


Solution

  • I assume that your data is already converted into the desired encoding (see @detunized answer).

    Using my answer to your previous question the conversion is straight forward:

    namespace io = boost::iostreams;
    
    io::filtering_wistream in;
    in.push(warray_source(array, arraySize));
    

    If you insist on not using boost then the conversion goes as follows (still straight forward):

    class membuf : public wstreambuf // <- !!!HERE!!!
    {
    public:
        membuf(wchar_t* p, size_t n) { // <- !!!HERE!!!
            setg(p, p, p + n);
        }
    };
    
    int main()
    {
        wchar_t buffer[] = L"Hello World!\nThis is next line\nThe last line";  
        membuf mb(buffer, sizeof(buffer)/sizeof(buffer[0]));
    
        wistream istr(&mb);
        wstring line;
        while(getline(istr, line))
        {
            wcout << L"line:[" << line << L"]" << endl;
        }
    }
    

    Also consider this for why use plain char UTF-8 streams.