文本和二进制模式下的 Unicode 流 I/OUnicode Stream I/O in Text and Binary Modes

当对一个以文本模式(默认方式)打开的文件执行 Unicode 流 I/O 例程(例如 fwprintffwscanffgetwcfputwcfgetwsfputws)时,将会发生两种字符转换:When a Unicode stream I/O routine (such as fwprintf, fwscanf, fgetwc, fputwc, fgetws, or fputws) operates on a file that is open in text mode (the default), two kinds of character conversions take place:

  • Unicode 转换为 MBCS 或 MBCS 转换为 Unicode。Unicode-to-MBCS or MBCS-to-Unicode conversion. 当 Unicode 流 I/O 函数在文本模式下运行时,源流或目标流将假定为一系列多字节字符。When a Unicode stream-I/O function operates in text mode, the source or destination stream is assumed to be a sequence of multibyte characters. 因此,Unicode 流输入函数将多字节字符转换为宽字符(就像调用 mbtowc 函数一样)。Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). 出于同一原因,Unicode 流输出函数将宽字符转换为多字节字符(就像调用 wctomb 函数一样)。For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

  • 回车-换行符 (CR-LF) 转换。Carriage return - linefeed (CR-LF) translation. 此转换发生在 MBCS 到 Unicode 的转换之前(对于 Unicode 流输入函数)和 Unicode 到 MBCS 的转换之后(对于 Unicode 流输出函数)。This translation occurs before the MBCS - Unicode conversion (for Unicode stream input functions) and after the Unicode - MBCS conversion (for Unicode stream output functions). 在输入过程中,每一个回车-换行符组合将转换为单一的换行符。During input, each carriage return - linefeed combination is translated into a single linefeed character. 在输出过程中,每一个换行符将转换为回车-换行符组合。During output, each linefeed character is translated into a carriage return - linefeed combination.

    但是,当 Unicode 流 I/O 函数以二进制模式运行时,该文件将被假定为 Unicode,并且在输入或输出过程中不会发生 CR-LF 转换或字符转换。However, when a Unicode stream-I/O function operates in binary mode, the file is assumed to be Unicode, and no CR-LF translation or character conversion occurs during input or output. 为了对 UNICODE 文本文件正确使用 wcin,请使用 _setmode( _fileno( stdin ), _O_BINARY ); 指令。Use the _setmode( _fileno( stdin ), _O_BINARY ); instruction in order to correctly use wcin on a UNICODE text file.

另请参阅See Also

按类别分的运行时例程 Run-Time Routines by Category
输入和输出Input and Output