CString 的 FormatMessageV() 将 \n 替换为 \r\n

FormatMessageV() of CString replaces \n with \r\n

提问人:ZoomIn 提问时间:10/30/2023 最后编辑:Andrew TruckleZoomIn 更新时间:10/31/2023 访问量:111

问:

有一个名为 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!";
}
C 可视化 C++ MFC 变量函数 行尾

评论

5赞 Some programmer dude 10/30/2023
你怎么知道它用 CR-NL 对替换换行符?你如何检查?您是否使用调试器来检查字符串的实际内容?您确定它实际上存在于字符串本身中,而不仅仅是打印或写入文本控制台或文件的工件,通常发生此类翻译?请尝试向我们展示您的测试用例的最小可重现示例
2赞 IInspectable 10/30/2023
由于 FormatMessageV 调用 FormatMessage,您是否尝试过使用?%n\n
1赞 IInspectable 10/30/2023
看起来像 的属性。如果不希望这样做,请使用调用标准 CRT 函数的 CString::FormatVFormatMessagevsprintf_s
2赞 Jabberwocky 10/30/2023
你确定首先需要@ZoomIn?FormatMessage
2赞 sergiol 10/31/2023
在 MFC 工作了 15 年以上,不记得我使用过一次此类或类似的功能......FormatMessageV

答: 暂无答案