How to use std :: wifstream to read its contents as std :: wstring

I am trying to do this:

std::wstringstream wstrStream;
std::wifstream wifStream(str.c_str());
wifStream >> wstrStream;

but I got this compilation error:

     error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::operator >>
(std::basic_istream<_Elem,_Traits>::_Myt &(__cdecl *)
(std::basic_istream<_Elem,_Traits>::_Myt &))' : cannot convert parameter 1 from
'std::wstringstream' to 'std::basic_istream<_Elem,_Traits>::_Myt &(__cdecl *)
(std::basic_istream<_Elem,_Traits>::_Myt &)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>
            ]
            and
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>
            ]

I understand that the → operator is not implemented for wchar_t.

I found some documentation and links to std :: wifstream. How do you use it?

+3
source share
2 answers

The → operator is not defined for two threads. If you want to read a line separated by spaces from a file, use

std::wstring s;
wifStream >> s;

If you want to copy the whole file to a line, use

wstrStream << wifStream.rdbuf();
+5
source

You do not need to use wstringstream anywhere - wifstream is a wstringstream under the hood. You just need to extract directly to std :: wstring.

+2
source

Source: https://habr.com/ru/post/1773851/


All Articles