c++ - fprintf 不打印 Unicode 字符

标签 c++ c visual-c++ mfc

<分区>

今天早上发现一个很奇怪的问题,一直找不到解决办法。请帮我用下面的代码解决这个问题(使用 Visual Studio C++ 和 MFC):

FILE *fp = NULL;
//fp = fopen(mmFileName , "a");
//Store the module directory path
fopen_s( &fp , "TempWorking.xml" , "wb");
//char* TempChar;
CString strtempff;
strtempff = "\"&lt;article&gt;  &lt;section class=&quot;page&quot;&gt;&lt;p&gt;Writing for the clown show that is &lt;em&gt;Forbes&lt;/em&gt;, Darcy Travlos asks the &lt;a href=&quot;http://en.wikipedia.org/wiki/Betteridge%27s_law_of_headlines&quot;&gt;Betteridge’s Law&lt;/a&gt;-challenging question “Apple And Google: The New ‘Old’ Reality?†(No link but tip o’ the an";
char* TempArray;
TempArray = new char[strtempff.GetLength()];
strcpy(TempArray,strtempff.GetString());
strtempff = "";
strtempff.Empty();
fprintf(fp,(char*)TempArray);

我不知道为什么我的代码在 fprintf() 语句中不起作用:它给了我一个调试断言。 strtempff 摘自网络,仅作为示例代码

最佳答案

我认为您的错误是您向 fprintf 提供的字符串不是格式字符串。你应该这样做:

fprintf(fp,"%s",(char*)TempArray);

解释一下,字符串中的任何 % 字符都会混淆它,因为它们是 printf 函数系列的特殊格式字符。关于缓冲区长度不足的其他评论也是正确的,您也需要修复它。

您可能还想使用 fwprintf 和宽字符串,因为您正在尝试打印扩展字符集。 http://www.cplusplus.com/reference/cwchar/fwprintf/

关于c++ - fprintf 不打印 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16736158/

相关文章:

计算 h 指数

c - 在 C 中发布增量查询

c - 如何只输入一个EOF结束scanf

windows - 如何查找发生运行时错误的行 - Visual C++

c++ - QDir.setNameFilter 如何仅显示具有特定扩展名的文件?

c++ - 声明指针;类型和名称之间空格的左侧或右侧的星号?

c++ - 如何在 C++ 中创建 3D 欧几里得点云和恒定距离网格

c++ - vs2012 工具集兼容性

c++ - 构建 websocket++ 时为 "src/common.hpp:52:32: fatal error: boost/shared_ptr.hpp: No such file or directory"

c++ - 堆上的内存是如何耗尽的?