mrg*_*mrg 2 c unix linux system-calls
我正在使用creat系统调用来创建一个文件.以下是创建文件的程序
#include<stdio.h>
#include<fcntl.h>
void main()
{
int fd=creat("a.txt",S_IRWXU|S_IWUSR|S_IRGRP|S_IROTH);
printf("fd = %d\n",fd);
}
Run Code Online (Sandbox Code Playgroud)
因此,程序第一次创建一个具有适当权限的名为a.txt的文件.如果我再次执行a.out,将创建新的a.txt.但是,文件的inode保持不变.怎么样,会的.
$ ./a.out
fd = 3
$ ls -li a.txt
2444 -rw-r--r-- 1 mohanraj mohanraj 0 Aug 27 15:02 a.txt
$ cat>a.txt
this is file a.txt
$ ./a.out
fd = 3
$ cat a.txt
$ls -li a.txt
2444 -rw-r--r-- 1 mohanraj mohanraj 0 Aug 27 15:02 a.txt
$
Run Code Online (Sandbox Code Playgroud)
在上面的输出中,a.txt的内容为"This is file a.txt".一旦我执行了a.out,就会创建新的a.txt.但是,inode 2444保持不变.那么,创建系统调用如何工作?
提前致谢.
creat仅在不存在的情况下创建文件.如果它已经存在,它就会被截断.
creat(filename, mode);
Run Code Online (Sandbox Code Playgroud)
相当于
open(filename, O_WRONLY|O_CREAT|O_TRUNC, mode);
Run Code Online (Sandbox Code Playgroud)
并且如open(2)文档中所述:
O_CREAT
如果文件不存在,将创建它.