Create a file foo and foo2 with the same time access and permissions using system programming. I am quite clueless about this, I had to create foo and foo2 outside, and now I am unsure whether this is the correct way to do it or not. This is what I have tried:
#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;
}
CodePudding user response:
As of file creation, the C command is open()
, as stated in it's man page (requires some libs, beware):
https://www.man7.org/linux/man-pages/man2/open.2.html
If the user wishes to create or override the file, he may use creat()
, which is in reality little more than open()
with these flags: O_CREAT|O_WRONLY|O_TRUNC
As for time access, others dwellers may be more helpful; Jabberwocky's link may be of some help.