Partager via


_setmode

Sets the file translation mode.

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

Remarks

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.

Warning

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>

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

.NET Framework Equivalent

See Also

Reference

File Handling

_creat, _wcreat

fopen, _wfopen

_open, _wopen

_set_fmode