为文件 I/O 操作设置默认文件转换模式。Sets the default file translation mode for file I/O operations.


errno_t _set_fmode(   
   int mode   


[in] mode[in] mode
所需的文件转换模式:_O_TEXT_O_BINARYThe file translation mode desired: _O_TEXT or _O_BINARY.

返回值Return Value

如果成功,则返回零;如果失败,则返回错误代码。Returns zero if successful, an error code on failure. 如果 mode 不是 _O_TEXT_O_BINARY_O_WTEXT,则将调用无效参数处理程序,如参数验证中所述。If mode is not _O_TEXT or _O_BINARY or _O_WTEXT, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则该函数将 errno 设置为 EINVAL 并返回 EINVALIf execution is allowed to continue, this function sets errno to EINVAL and returns EINVAL.


该函数设置 _fmode 全局变量。The function sets the _fmode global variable. 此变量为文件 I/O 操作 _open_pipe 指定默认的文件转换模式。This variable specifies the default file translation mode for the file I/O operations _open and _pipe.

_O_TEXT_O_BINARY 在 Fcntl.h 中进行定义。_O_TEXT and _O_BINARY are defined in Fcntl.h. EINVAL 在 Errno.h 中进行定义。EINVAL is defined in Errno.h.


例程Routine 必需的标头Required header 可选标头Optional header
_set_fmode <stdlib.h><stdlib.h> <fcntl.h>、<errno.h><fcntl.h>, <errno.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.


// crt_set_fmode.c  
#include <stdlib.h>  
#include <stdio.h>  
#include <fcntl.h>     /* for _O_TEXT and _O_BINARY */  
#include <errno.h>     /* for EINVAL */  
#include <sys\stat.h>  /* for _S_IWRITE */  
#include <share.h>     /* for _SH_DENYNO */  

int main()  
   int mode, fd, ret;  
   errno_t err;  
   int buf[12] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,  
                   75, 76 };  
   char * filename = "fmode.out";  

   err = _get_fmode(&mode);  
   if (err == EINVAL)  
      printf( "Invalid parameter: mode\n");  
      return 1;  
      printf( "Default Mode is %s\n", mode == _O_TEXT ? "text" :  

   err = _set_fmode(_O_BINARY);  
   if (err == EINVAL)  
      printf( "Invalid mode.\n");  
      return 1;  

   if ( _sopen_s(&fd, filename, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IWRITE | _S_IREAD) != 0 )  
      printf( "Error opening the file %s\n", filename);  
      return 1;  

   if (ret = _write(fd, buf, 12*sizeof(int)) < 12*sizeof(int))  
      printf( "Problem writing to the file %s.\n", filename);  
      printf( "Number of bytes written: %d\n", ret);  

   if (_close(fd) != 0)  
      printf("Error closing the file %s. Error code %d.\n",  
             filename, errno);  

   system("type fmode.out");  
Default Mode is binary  
A   B   C   D   E   F   G   H   I   J   K   L     

请参阅See Also

_fmode _fmode
_get_fmode _get_fmode
_setmode _setmode
文本和二进制模式文件 I/OText and Binary Mode File I/O