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 no开发者_运维技巧w 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 wstring
s. And I need this to be fast.
So anybody know how to treat this chunk of memory as a wistream
?
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.
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.
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.
精彩评论