I am trying out this code below.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd;
char filename[10];
printf("enter the file name\n");
scanf("%s",filename);
fd=open(filename,O_RDONLY|O_CREAT|O_TRUNC,S_IRUSR);
if(fd==-1)
{
printf("error opening file\n");
}
else
{
printf("file opened successfully\n");
}
return 0;
}
Here,I am settings file's permission such that only user can read this. When i execute this code for the first time it works as expected.But for the second time it shows the error message.My doubt is that why it is giving error message as I have already set the required permission mode in sync with flag modes.