提问人:ZoomIn 提问时间:10/30/2023 最后编辑:Andrew TruckleZoomIn 更新时间:10/31/2023 访问量:111
CString 的 FormatMessageV() 将 \n 替换为 \r\n
FormatMessageV() of CString replaces \n with \r\n
问:
有一个名为 FormatMessageV
的函数,它是 MSDN 所说的“使用变量参数列表格式化消息字符串”的类的一部分。
不幸的是,除了上述操作之外,它还替换了我不想要的消息。CString
\n
\r\n
我尝试传递 LF 的 ASCII 值而不是在消息中,但毫不奇怪,这并没有改变例程的行为。我在网上找不到任何提到为什么会发生这种情况以及如何预防它的内容。\n
你们这些好人对此有什么想法吗?下面是一个最小可重现的例子,不幸的是,我找不到任何免费的在线 Microsoft Visual C++ 编译器来运行示例和链接,但它在我的机器上的 Visual Studio C+ 项目中打印。Has CR in it!
#include <atlstr.h>
#include <iostream>
CString FormatV(LPCTSTR first, ...)
{
va_list vaList = nullptr;
CString buffer;
va_start(vaList, first);
buffer.FormatMessageV(first, &vaList); //this adds \r before \n
va_end(vaList);
return buffer;
}
int main()
{
CString text = L"\nint Fun(LPCTSTR commandLine, UINT number)\n{ Perform(commandLine,% 1); \n }"; // Notice that it has only \n s
CString formattedText = FormatV(text, 1); //formattedText has \r\n s
if (formattedText.Find(L'\r', 0) != -1)
std::cout << "Has CR in it!";
}
答: 暂无答案
评论
FormatMessageV
调用FormatMessage
,您是否尝试过使用?%n
\n
CString::FormatV
。FormatMessage
vsprintf_s
FormatMessage
FormatMessageV