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

别等时光非礼了梦想. 提交于 2019-12-09 22:47:18

问题


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?


回答1:


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.




回答2:


You cannot treat ASCII string as a UNICODE string, since the characters they contain have different sizes. So you would have to do some kind of conversion to a temporary buffer and then use that piece of memory as an input buffer for your stream. This is what you're doing right now.




回答3:


It should be obvious that if you have string, istream, and istringstream, therefore you also have wstring, wistream, and wistringstream.

Both istringstream and wistringstream are just specialization of the template class basic_istringstream for char and wchar respectively.



来源:https://stackoverflow.com/questions/4353894/how-can-i-read-from-memory-just-like-from-a-file-using-wistream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!