【问题标题】:System programming, create two files with same time access and permissions系统编程,创建两个同时访问和权限的文件
【发布时间】:2022-01-12 08:51:04
【问题描述】:

使用系统编程创建具有相同时间访问和权限的文件 foo 和 foo2。 我对此一无所知,我不得不在外面创建 foo 和 foo2 ,现在我不确定这是否是正确的方法。 这是我尝试过的:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include <fcntl.h>

int main()
{
    int fd = open("./foo", O_RDWR);
    
    int fd2 = open("./foo2", O_RDWR);
    if(fd == -1)
    {
        printf("\nError creating foo.");
    }
    else if(fd2 == -1)
    {
        printf("\nError creating foo2.");
    }
    else
    {
        printf("\nFile descripter 1:     %d", fd);  
        printf("\nFile descripter 2:     %d", fd2);
    }
    return 0;
}









【问题讨论】:

标签: c file


【解决方案1】:

创建文件时,C 命令为 open(),如其手册页所述(需要一些库,请注意):

https://www.man7.org/linux/man-pages/man2/open.2.html

如果用户希望创建或覆盖文件,他可以使用creat(),实际上它只是带有这些标志的open()O_CREAT|O_WRONLY|O_TRUNC

至于时间访问,其他居民可能更有帮助; Jabberwocky 的链接可能会有所帮助。

【讨论】:

  • 需要一些库 嗯? open() 几乎总是在隐式链接中,除非你告诉它,否则 libc.so 库。
猜你喜欢
  • 2011-07-23
  • 2014-08-04
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多