Top > C言語 > open


リニューアル

サイトリニューアル中です。本ページは以下URLへ移動しました。
Prog.さな雑記

関数仕様

書式
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
 
 
 
 
 
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int open (const char *filepath, int flag);
int open (const char *filepath, int flag, mode_t mode);
引数
filepath : オープンするファイルへのパス
flag     : 読み込み、書き込み、その他
mode     : 新規作成される場合のパーミッション設定
戻り値
0 以上の値 : ファイル記述子
-1         : エラー
フラグ
フラグ意味
O_RDONLY読み取り専用でオープン
O_WRONLY書き込み専用でオープン
O_RDWR読み書き両用でオープン
O_CREATファイルが存在しない場合は、新規作成する(引数modeの指定必要)

サンプル

すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
-
|
!
|
|
|
|
|
!
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
 
#define LOG_FILE_PATH "./test.log"
 
int
main (void)
{
  int fd, size;
  char buf[128];
 
  fd = open (LOG_FILE_PATH, (O_RDONLY | O_CREAT), 0664);
 
  if (fd < 0)
    {
      return -1;
    }
 
  /* 処理を書く */
 
  close (fd);
  return 0;
}

リロード   凍結解除 コピー 名前変更   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: Sun, 10 Oct 2021 04:40:44 UTC (1228d)