将C ++ LPCWSTR打印到文件 [英] Printing a C++ LPCWSTR to a file

查看:577
本文介绍了将C ++ LPCWSTR打印到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打印一个LPCWSTR值到文件,但它只打印地址,而不是值。

I'm trying to print an LPCWSTR value to a file, but it only prints the address, not the value.

我试图取消引用变量使用*)获取值,但这也不起作用。

I've tried dereferencing the variable (using *) to get the value , but this doesn't work either.

如何打印该值?

void dump(LPCWSTR text){

  ofstream myfile("C:\\myfile.txt", ios::app );
  myfile << text << endl;
  myfile.close();

}

提前感谢。

推荐答案

wofstream将生成一个也是unicode(没有BOM)的输出。这可能不是Brian想要的。

wofstream will generate an output that is also unicode (without a BOM). That may not be what Brian wants.

不幸的是,如果你想要你的文件是8位字符,你将走出C ++字符串并转换unicode字符串

Unfortunately if you want your file to be 8 bit characters, you're going to step out of C++ strings and convert the unicode strings into 8 bit characters.

您可以使用 wcstombs 将字符串转换为8位字符。转换是在当前语言环境中完成的,因此请确保使用 setlocale ,以确保您的转换在正确的区域设置中发生。不幸的是,setlocale的文档表明它不能转换为UTF-8:(

You can use wcstombs to convert the string to 8 bit characters. The conversion is done in the current locale, so make sure you use setlocale to ensure that your conversion happens in the correct locale. Unfortunately the documentation for setlocale indicates that it won't work to convert to UTF-8 :(

这篇关于将C ++ LPCWSTR打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆