how to use std::wifstream for reading its content as a std::wstring
Asked Answered
H

2

5

I am trying 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 operator >> is not implemented for wchar_t.

I found little documentation and references to std::wifstream. How would you use it ?

Handyman answered 9/11, 2010 at 14:24 Comment(1)
std::wstringstream? Do you mean std::wstring?Confer
G
7

Operator >> isn't defined for two streams. If you want to read a whitespace-delimited string from the file, use

std::wstring s;
wifStream >> s;

If you mean that you want to copy the entire file into the stringstream, use

wstrStream << wifStream.rdbuf();
Gibber answered 9/11, 2010 at 14:33 Comment(1)
Yep wstrStream << wifStream.rdbuf(); seems to be what I want.Handyman
Y
3

You don't need to use a wstringstream anywhere here - the wifstream is a wstringstream under the hood. You just need to extract directly into a std::wstring.

Yachting answered 9/11, 2010 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.