使用C在Linux中创建一个文件

时间:2021-05-23 01:19:32

I am trying to create a write only file in C on Linux (Ubuntu). This is my code:

我正在尝试在Linux (Ubuntu)上创建一个只用C写的文件。这是我的代码:

 int fd2 = open ("/tmp/test.svg", O_RDWR|O_CREAT);

 if (fd2 != -1) {
   //....
 }

But why do the files I created have 'xr' mode? How can I create it so that I can open it myself at command prompt?

但是为什么我创建的文件有“xr”模式?如何创建它,以便我可以在命令提示符下自己打开它?

------xr--  1 michael michael  55788 2010-03-06 21:57 test.txt*
------xr--  1 michael michael   9703 2010-03-06 22:41 test.svg*

2 个解决方案

#1


32  

You need the three-argument form of open() when you specify O_CREAT. When you omit the third argument, open() uses whatever value happens to be on the stack where the third argument was expected; this is seldom a coherent set of permissions (in your example, it appears that decimal 12 = octal 014 was on the stack).

在指定O_CREAT时,需要open()的三参数形式。当您省略第三个参数时,open()使用栈中第三个参数所期望的值;这很少是一组连贯的权限集(在您的示例中,似乎是在堆栈上的decimal 12 = octal 014)。

The third argument is the permissions on the file - which will be modified by the umask() value.

第三个参数是文件的权限,它将被umask()值修改。

int fd2 = open("/tmp/test.svg", O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);

Note that you can create a file without write permissions (to anyone else, or any other process) while still being able to write to it from the current process. There is seldom a need to use execute bits on files created from a program - unless you are writing a compiler (and '.svg' files are not normally executables!).

注意,您可以创建一个文件,而无需写入权限(对其他人或任何其他进程),同时仍然能够从当前进程写入它。很少需要对程序创建的文件使用执行位——除非您正在编写一个编译器(和)。svg的文件通常不是可执行的!

The S_xxxx flags come from <sys/stat.h> and <fcntl.h> — you can use either header to get the information (but open() itself is declared in <fcntl.h>).

S_xxxx标志来自 和< fcntl。h> -可以使用任何一个header来获取信息(但open()本身在 中声明)。

Note that the fixed file name and the absence of protective options such as O_EXCL make even the revised open() call somewhat unsafe.

注意,固定的文件名和诸如O_EXCL之类的保护选项的缺失使得修改后的open()调用有点不安全。

#2


1  

Give access permissions as the third parameter:

第三个参数为访问权限:

int fd2 = open("/tmp/test.svg", O_RDWR|O_CREAT, 0777);  // Originally 777 (see comments)

if (fd2 != -1) {
    // use file descriptor
    close(fd2);
}

By doing this, all read, write and execute permissions will be given to user, group and others. Modify the 3rd parameter according to your use.

通过这样做,所有的读、写和执行权限将被授予用户、组和其他人。根据使用情况修改第3个参数。

#1


32  

You need the three-argument form of open() when you specify O_CREAT. When you omit the third argument, open() uses whatever value happens to be on the stack where the third argument was expected; this is seldom a coherent set of permissions (in your example, it appears that decimal 12 = octal 014 was on the stack).

在指定O_CREAT时,需要open()的三参数形式。当您省略第三个参数时,open()使用栈中第三个参数所期望的值;这很少是一组连贯的权限集(在您的示例中,似乎是在堆栈上的decimal 12 = octal 014)。

The third argument is the permissions on the file - which will be modified by the umask() value.

第三个参数是文件的权限,它将被umask()值修改。

int fd2 = open("/tmp/test.svg", O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);

Note that you can create a file without write permissions (to anyone else, or any other process) while still being able to write to it from the current process. There is seldom a need to use execute bits on files created from a program - unless you are writing a compiler (and '.svg' files are not normally executables!).

注意,您可以创建一个文件,而无需写入权限(对其他人或任何其他进程),同时仍然能够从当前进程写入它。很少需要对程序创建的文件使用执行位——除非您正在编写一个编译器(和)。svg的文件通常不是可执行的!

The S_xxxx flags come from <sys/stat.h> and <fcntl.h> — you can use either header to get the information (but open() itself is declared in <fcntl.h>).

S_xxxx标志来自 和< fcntl。h> -可以使用任何一个header来获取信息(但open()本身在 中声明)。

Note that the fixed file name and the absence of protective options such as O_EXCL make even the revised open() call somewhat unsafe.

注意,固定的文件名和诸如O_EXCL之类的保护选项的缺失使得修改后的open()调用有点不安全。

#2


1  

Give access permissions as the third parameter:

第三个参数为访问权限:

int fd2 = open("/tmp/test.svg", O_RDWR|O_CREAT, 0777);  // Originally 777 (see comments)

if (fd2 != -1) {
    // use file descriptor
    close(fd2);
}

By doing this, all read, write and execute permissions will be given to user, group and others. Modify the 3rd parameter according to your use.

通过这样做,所有的读、写和执行权限将被授予用户、组和其他人。根据使用情况修改第3个参数。