Cette documentation est archivée et n’est pas conservée.


Sets the file translation mode.

int _setmode (
   int fd,
      int mode 



File descriptor.


New translation mode.

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. If execution is allowed to continue, this function returns –1 and sets errno to either EBADF, indicating an invalid file descriptor, or EINVAL, indicating an invalid mode argument (neither _O_TEXT nor _O_BINARY).

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

The _setmode function sets to mode the translation mode of the file given by fd. The mode must be one of two manifest constants: _O_TEXT or _O_BINARY. _O_TEXT sets text (translated) mode. Carriage return–line feed (CR-LF) combinations are translated into a single line feed character on input. Line feed characters are translated into CR-LF combinations on output. _O_BINARY sets binary (untranslated) mode, in which these translations are suppressed.

_setmode is typically used to modify the default translation mode of stdin and stdout, but you can use it on any file. If you apply _setmode to the file descriptor for a stream, call _setmode before performing any input or output operations on the stream.

Routine Required header Optional Headers Compatibility




ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For more compatibility information, see Compatibility in the Introduction.

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