设置文件转换模式。Sets the file translation mode.


int _setmode (
   int fd,
   int mode


文件描述符。File descriptor.

新转换模式。New translation mode.

返回值Return Value

如果成功,将返回之前的转换模式。If successful, returns the previous translation mode.

如果传递到此函数的参数无效,则将调用无效参数处理程序,如参数验证中所述。If invalid parameters are passed to this function, the invalid-parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则此函数将返回-1,并将errno设置为ebadf ( ,指示无效的文件描述符,或设置为EINVAL,指示无效的模式参数。If execution is allowed to continue, this function returns -1 and sets errno to either EBADF, which indicates an invalid file descriptor, or EINVAL, which indicates an invalid mode argument.

有关这些属性和其他的更多信息返回代码示例,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerrFor more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.


_Setmode函数将fd指定的文件的转换模式设置为modeThe _setmode function sets to mode the translation mode of the file given by fd. _O_TEXT作为模式传递会设置文本(即转换后的模式)。Passing _O_TEXT as mode sets text (that is, translated) mode. 回车符(CR-LF)组合在输入时转换为换行符。Carriage return-line feed (CR-LF) combinations are translated into a single line feed character on input. 输出时换行符将转换为 CR-LF 组合。Line feed characters are translated into CR-LF combinations on output. 传递 _O_BINARY会设置二进制(未转换)模式,在此模式下,将禁止显示这些翻译。Passing _O_BINARY sets binary (untranslated) mode, in which these translations are suppressed.

你还可以传递 _O_U16TEXT_O_U8TEXT_O_WTEXT ,以启用 Unicode 模式,如本文档后面的第二个示例中所示。You can also pass _O_U16TEXT, _O_U8TEXT, or _O_WTEXT to enable Unicode mode, as demonstrated in the second example later in this document.


Unicode 模式适用于宽打印功能(例如, wprintf),而窄打印功能则不支持。Unicode mode is for wide print functions (for example, wprintf) and is not supported for narrow print functions. 对 Unicode 模式流使用窄显函数会触发断言。Use of a narrow print function on a Unicode mode stream triggers an assert.

_setmode通常用于修改stdinstdout的默认转换模式,但是可以在任何文件上使用它。_setmode is typically used to modify the default translation mode of stdin and stdout, but you can use it on any file. 如果将 _setmode应用到流的文件描述符,请在对流执行任何输入或输出操作之前调用 _setmodeIf you apply _setmode to the file descriptor for a stream, call _setmode before you perform any input or output operations on the stream.


如果将数据写入文件流,请先使用fflush显式刷新代码,然后再使用 _setmode更改模式。If you write data to a file stream, explicitly flush the code by using fflush before you use _setmode to change the mode. 如果不刷新代码,可能会导致意外行为。If you do not flush the code, you might get unexpected behavior. 如果尚未将数据写入流,则不必刷新代码。If you have not written data to the stream, you do not have to flush the code.


例程所返回的值Routine 必需的标头Required header 可选标头Optional Headers
_setmode_setmode <io.h><io.h> <fcntl.h><fcntl.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_setmode.c
// This program uses _setmode to change
// stdin from text mode to binary mode.

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main( void )
   int result;

   // Set "stdin" to have binary mode:
   result = _setmode( _fileno( stdin ), _O_BINARY );
   if( result == -1 )
      perror( "Cannot set mode" );
      printf( "'stdin' successfully changed to binary mode\n" );
'stdin' successfully changed to binary mode


// crt_setmodeunicode.c
// This program uses _setmode to change
// stdout to Unicode. Cyrillic and Ideographic
// characters will appear on the console (if
// your console font supports those character sets).

#include <fcntl.h>
#include <io.h>
#include <stdio.h>

int main(void) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
    return 0;

请参阅See also

文件处理File Handling
_creat、_wcreat_creat, _wcreat
fopen、_wfopen_wfopenfopen, _wfopen
_open、_wopen_open, _wopen