0

I am currently using the following line to get a file descriptor:

int fd = open(file_name, O_RDWR | O_APPEND | O_CREAT);

but it fails (** fd = -1 with Error: permission denied**) when the file already exists. I am using the access() function to see if the file already exists:

if (access(file_name, F_OK) != -1)
{
   printf("file %s already exists! \n", file_name);
   remove(file_name);  /* delete the existing file */
   fd = open(file_name, O_RDWR | O_APPEND | O_CREAT);
}

Is there a better way to use the open() function to get a file descriptor for the following two scenarios:

  1. if the file does not exist, create it and return a file descriptor
  2. if the file exists, delete it and create a new file, then return a file descriptor.
5
  • File permissions - have you checked that? Commented Apr 19, 2014 at 17:30
  • if I chane the file permission to 0777, no error reported. Commented Apr 19, 2014 at 17:31
  • The permission denied error is because you don't have permissions to open or create the file in the directory where you are executing your program. Try to run it with sudo (if this is possible). Also the funtion open works in the scenarios you said. But if you don't want to work in a low level (with file descriptors) you can work with this C standar library cplusplus.com/reference/cstdio/FILE. Although you can do what you want with both. Commented Apr 19, 2014 at 17:33
  • @TonyGW - Is that the flle, directory, sub directories etc. Why not be that user and use the shell command echo "hello" >hello.txt Commented Apr 19, 2014 at 17:35
  • Change the attributes of file. You may not be having the permissions to remove that file. Commented Apr 19, 2014 at 17:41

3 Answers 3

4

if the file exists, delete it and create a new file, then return a file descriptor.

That's not supported by open(). The closest thing to that is O_TRUNC, which truncates the file if it exists, but there's no O_ flag that deletes the file if it exists, and you need write permissions for the file to truncate it.

As others have noted, the real problem you probably want to fix is your lack of write permissions for the file. If you own the file, you should change the permissions if you want to write to it; if you don't own the file, presumably the owner doesn't want you to write to it, so you shouldn't write to it.

Sign up to request clarification or add additional context in comments.

Comments

1

The open system call works in both scenarios, where you have the documentation http://codewiki.wikidot.com/c:system-calls:open. Also, the permission denied error occurs because you don't have permissions to open the file or create it in the directory where you are running your program, try to run it as sudo. If you don't want to work with system calls, theres other C library (it's more high level, you don't need to work with file descriptors), here you have a documentation of this one: http://www.cplusplus.com/reference/cstdio/FILE/

Note: you can also change the permissions of your user to be able to access the file or create it on the directory see chmod command on linux.

Comments

0

Thanks for all the replies. This single line of code solved my problem:

int fd = open(file_name, O_RDWR | O_APPEND | O_CREAT, 0777);  /* change the file permission to 0777 */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.