リニューアル
サイトリニューアル中です。本ページは以下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;
}
|