open()系统调用中的第三个参数如何工作?

问题描述 投票:-1回答:1
int fd = open("float.txt", O_CREAT | O_WRONLY, 0600);

我知道0600与权限有关,但它究竟是如何工作的?

c unix
1个回答
2
投票

使用O_CREAT标志时,您需要使用3 param版本的open

根据the man page for open

int open(const char * pathname,int flags);

int open(const char * pathname,int flags,mode_t mode);

mode参数指定在创建新文件时应用的文件模式位。当在flags中指定O_CREAT或O_TMPFILE时,必须提供此参数;如果既未指定O_CREAT也未指定O_TMPFILE,则忽略mode。

试试这个:

int fd = open("float.txt", O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR);
© www.soinside.com 2019 - 2024. All rights reserved.