open() 函数参数 [英] open() function parameters
问题描述
如果您通过考虑最后一个参数0"来查看下面的代码块,写行是否正常工作?
If you look at this code block below by taking into consideration the last parameter "0", Does write line work properly ?
filename = argv[1];
string = "Example string";
if (stat(argv[1], &buf) != 0)
{
fd = open(filename, O_WRONLY | O_CREAT, 0);
if (fd < 0)
{
perror(filename);
exit(1);
}
write(fd, string, strlen(string));
close(fd);
}
else
{
print("%s file exists\n", filename);
}
推荐答案
来自手册页:
mode
指定在创建新文件时使用的权限.在标志中指定 O_CREAT
时必须提供此参数;如果未指定 O_CREAT
,则忽略 mode
.进程的 umask 以通常的方式修改有效权限:创建的文件的权限是 (mode & ~umask
).请注意,此模式仅适用于新创建的文件的未来访问;创建只读文件的 open()
调用很可能会返回一个读/写文件描述符.
mode
specifies the permissions to use in case a new file is created. This argument must be supplied whenO_CREAT
is specified in flags; ifO_CREAT
is not specified, thenmode
is ignored. The effective permissions are modified by the process's umask in the usual way: The permissions of the created file are (mode & ~umask
). Note that this mode applies only to future accesses of the newly created file; theopen()
call that creates a read-only file may well return a read/write file descriptor.
为模式提供了以下符号常量:
The following symbolic constants are provided for mode:
S_IRWXU 00700 user (file owner) has read, write and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
因此,将 mode
指定为零,您将创建一个文件,其权限为 0 &~umask
,即一个没有任何权限的文件.
So, specifying a mode
of zero, you will create a file with the permissions of 0 & ~umask
, i.e. a file without any permissions.
文件系统的具体内容不在 open()
或 write()
函数的范围内.
What exactly the filesystem makes of this is not in the domain of the open()
or write()
functions.
这篇关于open() 函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!