_setmode_setmode

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

语法Syntax

int _setmode (  
   int fd,  
   int mode   
);  

参数Parameters

fd
文件描述符。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,指示无效mode自变量。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 函数将 mode 设置为由 fd 给定的文件的转换模式。The _setmode function sets to mode the translation mode of the file given by fd. 通过将 _O_TEXT 传递为 mode 可设置文本(即转换)模式。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.

Caution

如果将数据写入文件流,请先通过使用 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 <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 fopen, _wfopen
_open、_wopen _open, _wopen
_set_fmode_set_fmode