_setmode_setmode

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

语法Syntax

int _setmode (
   int fd,
   int mode
);

参数Parameters

fdfd
文件描述符。File descriptor.

模式mode
新转换模式。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 并设置errnoEBADF,指示无效文件描述符或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.

备注Remarks

_Setmode函数将设置为模式通过指定文件的转换模式fdThe _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. _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流的文件描述符,调用 _setmode执行在流上的任何输入或输出操作之前。If 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.

要求Requirements

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

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

示例Example

// 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" );
   else
      printf( "'stdin' successfully changed to binary mode\n" );
}
'stdin' successfully changed to binary mode

示例Example

// 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
_set_fmode_set_fmode