C++
1#include <atlstr.h>
2#include <sstream>
3#include <iostream>
4
5using tstringstream = std::basic_stringstream<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR>>;
6using tostream = std::basic_ostream<TCHAR, std::char_traits<TCHAR>>;
7using tistream = std::basic_istream<TCHAR, std::char_traits<TCHAR>>;
8
9int main()
10{
11 CString w_str("文字列");
12 CString r_str;
13 int size = w_str.GetLength() + 1;
14
15 tstringstream s;
16 tostream& os(s);
17 os.write(w_str.GetString(), size);
18
19 tistream& is(s);
20 is.read(r_str.GetBuffer(size), size);
21 r_str.ReleaseBuffer();
22
23 return 0;
24}
文字セットの設定がUNICODE時にstringstreamが文字化けしていてもいいなら以下でも通りますが…
C++
1#include <atlstr.h>
2#include <sstream>
3#include <iostream>
4
5int main()
6{
7 CString w_str("文字列");
8 CString r_str;
9 int size = (w_str.GetLength() + 1) * sizeof(TCHAR);
10
11 std::stringstream s;
12 std::ostream& os(s);
13 os.write((const char*)w_str.GetString(), size);
14
15 std::istream& is(s);
16 is.read((char*)r_str.GetBuffer(size), size);
17 r_str.ReleaseBuffer();
18
19 return 0;
20}
退会済みユーザー
2020/09/24 11:49