返回信息流使用Unicode
因为用的宽字符,所以使用wfstream和wstring、wsstream。但是从文件中使用getline读入一行到line_buffer中正常,但用line_buffer转换到wsstream后数据就变了。再往int里写结果就不对了。该怎么做呢?望指教,谢谢!
代码如下:
1 #include "sstream"
2 #include "fstream"
3 #include "iostream"
4
5 using namespace std;
6
7 int main()
8 {
9 std::wofstream load_file ( "test.dat" );
10 std::wstring line_buffer;
11 std::wstringstream buffer;
12
13 if ( !load_file )
14 std::wcerr << L"not opened" << std::endl;
15
16 for ( int i = 0; i != 10; ++i )
17 load_file << i << endl;
18
19 load_file.close();
20
21 std::wifstream read_file ( "test.dat" );
22 while (read_file)
23 {
24 int i;
25 getline(read_file, line_buffer);
26 buffer.str(line_buffer);
27 buffer >> i;
28 wcerr << i << endl;
29 }
30
31 return 0;
32 }
另外再问个问题:
fstream不能通过wstring指定路径打开,但wstring只能通过c_str()转换为wchar_t*,要转换为char*还要费点功夫。不知道有什么好一点的方法。不是说用unicode移植性好吗?但文件路径不能通过unicode指定的话什么不都白搭?这种情况该怎么做呢?谢谢!
这是一条镜像帖。来源:北邮人论坛 / cpp / #7487同步于 2008/5/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[求助]关于使用wstringstream处理字符串
FadeToBlack
2008/5/25镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。