如何使用 std::wifstream 将其内容读取为 std::wstring
我正在尝试这个:
std::wstringstream wstrStream;
std::wifstream wifStream(str.c_str());
wifStream >> wstrStream;
但我得到了这个编译错误:
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>
]
我理解运算符>>未针对 wchar_t 实现。
我发现很少有文档和对 std::wifstream 的引用。 你会如何使用它?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运算符>>没有为两个流定义。如果您想从文件中读取以空格分隔的字符串,请使用
如果您想将整个文件复制到字符串流中,请使用
Operator >> isn't defined for two streams. If you want to read a whitespace-delimited string from the file, use
If you mean that you want to copy the entire file into the stringstream, use
您不需要在此处的任何地方使用 wstringstream - wifstream 是底层的 wstringstream。您只需直接提取到 std::wstring 中即可。
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.