IrishDog
IrishDog

Reputation: 470

Creating a file with open() but have not permissions to modify it in C

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

Answers (2)

FrUh
FrUh

Reputation: 555

There should be problem in flags, because you are creating file, you are using flag

O_CREAT
so now you need to specify access mode, as mentioned in man page. You specified just
S_IROTH
So 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_IWUSR
Se man page for details.

Upvotes: 1

Anis_Stack
Anis_Stack

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

Related Questions