CStdioFile::WriteString 为换行添加额外的回车 return?

CStdioFile::WriteString adding additional carriage return for line feeds?

我刚刚遇到以下行为:使用 CStdioFile::WriteString 时,它会将 \n 转换为 \r\n。 我在我的代码的早期版本中没有注意到这种行为,只是在我将我的项目转换为 Unicode 之后。我错过了什么?

我试过这段代码:

        CStdioFile file;
        CFileException fileException;

        file.Open(TEXT("c:\test.txt"), CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive | CFile::typeText, &fileException);

        file.WriteString(TEXT("__CR\r"));
        file.WriteString(TEXT("__LF\n"));
        file.WriteString(TEXT("CRLF\r\n"));
        file.WriteString(TEXT("____"));
        file.Flush();
        file.Close();

结果输出现在是:

__CR\r
__LF\r\n   //??
CRLF\r\r\n //??
____

我自己找到了答案:这是 CFile::typeText 的设计行为。

MSDN documentation 状态:

Text mode provides special processing for carriage return–linefeed pairs. When you write a newline character (0x0A) to a text-mode CStdioFile object, the byte pair (0x0D, 0x0A) is sent to the file. When you read, the byte pair (0x0D, 0x0A) is translated to a single 0x0A byte.

所以实际上我将模式从 CFile::typeBinary 更改为 CFile::typeText,从而引发了另一种行为。对于 CFile::typeBinary,您将不会获得额外的运输 returns。