c++ - 如何将 CString 转换为 ofstream?

标签 c++

我有几个 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;

关于c++ - 如何将 CString 转换为 ofstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5550838/

相关文章:

返回不可变指针 vector 的函数的 C++ 语法

c++ - C++ 中的字符串 : Problems with good() and get()

c++ - 将自定义上下文菜单添加到托管 Web 浏览器控件

c++ - 用于模型检查大型分布式 C++ 项目(如 KDE)的工具?

c++ - 是否有任何关于 Windows NT Native API 的最新书籍或网站?

c++ - 如何在C++中将JWK公钥转换为PEM格式

c++ - getter setter 中的互斥量

c++ - 更快的导出嵌入数据的方法

c++二进制到十进制转换器输入仅接受1或0

c++ - 是否可以在其范围之外访问局部变量的内存?