【问题标题】:Open system call in unix在 unix 中打开系统调用
【发布时间】:2015-08-26 09:24:50
【问题描述】:

在 unix 中打开系统调用

以下是开放系统调用的原型:

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

pathname --> 用于提及/home/mohan/a.txt等文件的路径

flags --> 用于指明文件将以何种模式打开,如只读、只写或读写。

模式 --> ?

什么是模式以及我何时使用该模式。有没有使用模式的例子。

提前致谢。

【问题讨论】:

  • 你可能想检查这个问题Using the open system call
  • 阅读文档怎么样?在终端中键入man open。否则,请在您最喜欢的互联网搜索引擎中输入 man open

标签: c linux unix


【解决方案1】:

正如我们在open(2) man page 中看到的,该模式用于设置文件的访问权限(就像您可以使用 UNIX 命令chmod 一样)。

不创建文件的时候没用,但是使用标志O_CREAT的时候就得用了。

示例:在读写模式下创建权限为 644 的文件:

int fd = open("file", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多