C中打开文件功能中的标志和模式之间的关系是什么 [英] What's the connection between flags and mode in open file function in C

查看:83
本文介绍了C中打开文件功能中的标志和模式之间的关系是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的初学者,对C语言中打开文件功能中的标志和模式参数有疑问 所以C的open函数是:

I'm a beginner in C, have a question about the flags and mode paramaters in open file function in C so C's open function is :

int open(char *filename, int flags, mode_t mode);

和标志的一些宏是:

  • O_RDONLY:只读
  • O_WRONLY:仅写
  • O_RDWR:读写
  • O_RDONLY: Reading only
  • O_WRONLY: Writing only
  • O_RDWR: Reading and writing

和模式位类似:

我不明白的是, 假设我们有一个开放功能,如:

What I don't understand is, let say we have a open function as:

fd = Open("foo.txt", O_RDONLY, S_IWOTH);

所以O_RDONLY指定我们只能读取文件,但是S_IWOTH指定任何人都可以写入该文件,这不是彼此矛盾吗?

so O_RDONLY specifies that we can only read the file, but S_IWOTH specifies that anyone can write this file, isn't that they contradict to each other?

推荐答案

标志决定此时在打开此文件的过程中要应用的属性(让我们调用这就是会话")-这会影响文件打开时的处理方式(或更正确地说,是文件描述符的处理方式).

The flags decide the properties to be applied during opening of this file at this time (let's call this the "session") - this affects what you can do with the file while it's open (or, more correctly, what you can do with the file descriptor).

模式决定在打开过程中创建文件的属性-这会影响以后任何人打开文件的方式.

The mode decide the properties of the file should it be created as part of the opening process - this affects how anyone can open the file in future.

您的特定示例(尽管使用正确的open而不是Open):

Your specific example (albeit with the correct open rather than Open):

fd = open("foo.txt", O_RDONLY, S_IWOTH);

并不是真正相关的,因为不会在没有O_CREAT标志(a)的情况下创建文件 .

is not really relevant since the file won't be created without the O_CREAT flag(a).

但是,如果您提供了O_CREAT,则完全可以接受创建文件,该文件允许任何人对其进行写操作,但是要以只读模式在此会话中打开该文件.

However, had you supplied O_CREAT, it's perfectly acceptable to create the file allowing anyone to write to it, but have it opened for this session in read-only mode.

(a)某些系统具有 other 标志,这些标志在某些情况下可能会创建文件.例如,Linux具有O_TMPFILE标志.

(a) Some systems have other flags which may create the file under some circumstances. For example, Linux has the O_TMPFILE flag.

这篇关于C中打开文件功能中的标志和模式之间的关系是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆