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。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭 選擇性標頭
_set_fmode <stdlib.h> <fcntl.h > 、 < errno.h>

如需相容性詳細資訊,請參閱相容性

範例

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

另請參閱

_fmode
_get_fmode
_setmode
文字和二進位模式檔案 I/O