The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
We recommend using Visual Studio 2017



The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at _setmode.

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, which indicates an invalid file descriptor, or EINVAL, which indicates an invalid mode argument.

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. Passing _O_TEXT as mode sets text (that is, 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. Passing _O_BINARY sets binary (untranslated) mode, in which these translations are suppressed.

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 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 you perform any input or output operations on the stream.

System_CAPS_ICON_caution.jpg Caution

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.

RoutineRequired headerOptional Headers

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;  

File Handling
_creat, _wcreat
fopen, _wfopen
_open, _wopen