Share via


_set_fmode

既定のファイルをファイル I/O 操作の変換モードを設定します。

errno_t _set_fmode( 
   int mode 
);

パラメーター

  • [入力] mode
    必要なファイルの変換モード : _O_TEXT または _O_BINARY。

戻り値

は失敗した場合はエラー コードを正常終了した場合はを返します。mode が _O_TEXT または _O_BINARY または _O_WTEXT 場合無効なパラメーター ハンドラーが パラメーターの検証 に説明されているように開始されます。実行の継続が許可された場合関数のセット errno は EINVAL を返しEINVALこの。

解説

関数のセット _fmode のグローバル変数です。この変数にはファイル I/O 操作 _open と _pipe の変換モード既定のファイルを指定します。

_O_TEXT と _O_BINARY は Fcntl.h で定義されます。EINVAL はErrno.h で定義されます。

必要条件

ルーチン

必須ヘッダー

オプション ヘッダー

_set_fmode

<stdlib.h>

<fcntl.h><errno.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

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

参照

関連項目

_fmode

_get_fmode

_setmode

テキスト モードとバイナリ モードのファイル入出力