_set_fmode_set_fmode

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

语法Syntax

errno_t _set_fmode( 
   int mode 
);

参数Parameters

模式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. 如果模式_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. 如果允许执行继续,此函数将errnoEINVAL并返回EINVALIf execution is allowed to continue, this function sets errno to EINVAL and returns EINVAL.

备注Remarks

该函数设置 _fmode 全局变量。The function sets the _fmode global variable. 此变量指定的文件 I/O 操作的默认文件转换模式 _open_pipeThis 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.

要求Requirements

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

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

// 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;
   }
   else
      printf( "Default Mode is %s\n", mode == _O_TEXT ? "text" :
              "binary");

   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