Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System call open C can't print file content if the file is not opened as sudo

So I create a new file:

 fd = open("tester.txt", O_CREAT | O_RDWR);

then using the system call write I add some info to it. But when I try to read the info from the file, it can't be made. Using the terminal I found out, that the only way to open the file is to use sudo and the content is successfully written. However, my program can't be root. So, how do I open the file, write some content to it and without closing the C program output the file.

like image 907
Karina Kozarova Avatar asked Jan 03 '23 04:01

Karina Kozarova


1 Answers

You are missing to specify the file mode as third argument to the creating open call; try the following:

fd = open("tester.txt", O_CREAT | O_RDWR, 0644);

Then, the file should be created with mode -rw-r--r--, so your own user can open it for reading and writing. Otherwise, it might end up with some random permission, i.e. ---------, and only root can open this for reading (without chmodding it, at least).

like image 109
Karim Avatar answered Jan 05 '23 17:01

Karim