Visual Studio 6.0

Sets the file translation mode.

int _setmode ( int handle, int mode );

Routine Required Header Optional Headers Compatibility
_setmode <io.h> <fcntl.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

If successful, _setmode returns the previous translation mode. A return value of –1 indicates an error, in which case errno is set to either EBADF, indicating an invalid file handle, or EINVAL, indicating an invalid mode argument (neither _O_TEXT nor _O_BINARY).



File handle


New translation mode


The _setmode function sets to mode the translation mode of the file given by handle. The mode must be one of two manifest constants, _O_TEXT or _O_BINARY. _O_TEXT sets text (translated) mode. Carriage return–linefeed (CR-LF) combinations are translated into a single linefeed character on input. Linefeed 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 handle for a stream, call _setmode before performing any input or output operations on the stream.


/* SETMODE.C: This program uses _setmode to change
 * stdin from text mode to binary mode.

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

void 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

File Handling Routines

See Also   _creat, fopen, _open