使用open syscall编写和创建文件,文件没有属性。 fedora16 gcc-4.6.3
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char * str= "helloworld";
int fd = open("test.db",O_WRONLY|O_CREAT|O_TRUNC|O_APPEND);
write(fd,str,10);
close(fd);
return 0;
}
ll test.db
----------。 1 jiamo jiamo 14 Apr 17 11:34 test.db
虽然它不会使用touch test.db
umask:0002
如果丢弃O_TRUNC
int fd = open("test1.db",O_WRONLY|O_CREAT|O_APPEND)
文件属性是:
---- --- RWX。 1 jiamo jiamo 14 Apr 17 12:29 test1.db
答案 0 :(得分:3)
将所需权限添加到open()系统调用:
int fd = open("test.db",O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0666);
来自文档:
mode must be specified when O_CREAT is in the flags, and is ignored otherwise.
The argument mode specifies the permissions to use in case a new file is created.
答案 1 :(得分:1)