Reputation: 470
I'am creating a text file in C that way
fd = open("results.txt",O_CREAT|O_RDWR,S_IROTH);
if (fd<0){
perror("open error");
abort();
}
write(fd,msg,strlen(msg)+1);
But when I'm trying to write something else in it I have not permissions . Could anyone help ?
Upvotes: 1
Views: 239
Reputation: 555
There should be problem in flags, because you are creating file, you are using flag
O_CREATso now you need to specify access mode, as mentioned in man page. You specified just
S_IROTHSo you made it just readonly for others. So when you open it again you CAN'T write. So you should also define write modes, also read for user etc.
S_IRUSR | S_IROTH | S_IWUSRSe man page for details.
Upvotes: 1
Reputation: 3452
use fd=open("results.txt",O_CREAT|O_RDWR,0777);
instead of
fd = open("results.txt",O_CREAT|O_RDWR,S_IROTH);
Upvotes: 2