如何将CString转换为ofstream
How to convert CString to ofstream?
我有一些CString变量,我希望输出到ofstream,但我似乎无法弄清楚如何做到这一点。下面是一些示例代码:
我在VS 2005中启用了unicode
代码: ofstream ofile;
CString str = "some text";
ofile.open(filepath);
ofile << str; // doesn't work, gives me some hex value such as 00C8F1D8 instead
ofile << str.GetBuffer(str.GetLength()) << endl; // same as above
ofile << (LPCTSTR)str << endl; // same as above
// try copying CString to char array
char strary[64];
wcscpy_s(strary, str.GetBuffer()); // strary contents are correctly copied
ofile << strary << endl ; // same hex value problem again!
任何想法?谢谢!
如果使用UNICODE,为什么不使用wofstream,它是一个由wchar_t参数化的basic_stream。如下图所示:
CString str = _T("some text");
std::wofstream file;
file.open(_T("demo.txt"));
file << (LPCTSTR)str;
如果您只想要纯ACP编码的ANSI文本:
ofile << CT2A(str);
ofstream格式输出函数期望窄/ansi字符串。
cstring表示char字符串
CT2A宏转换TCHAR 2 Ansi.
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- fstream / ifstream / ofstream 对象如何转换为布尔值
- C++线程:无法将参数 1 从 'const std::ofstream' 转换为 'std::ofstream &'
- ofstream:无法转换为ofstream&
- 隐式强制转换为 basic_istream/ifstream/ofstream 的布尔值在 Visual Studio 2013 中不起作用
- 如何将CString转换为ofstream