如果您在考虑最后一个参数“0”的情况下查看下面的代码块,写行是否正常工作?
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
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
权限的文件,即文件 < strong>没有任何权限。
文件系统究竟做了什么不在 open()
或 write()
函数的范围内。
关于c - open() 函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37543659/