audiFanatic
audiFanatic

Reputation: 2524

Check if a file is opened by another application in C?

I am trying to write a program which will encrypt a file and save the result to another file. As part of the assignment, we are not allowed to use standard C library functions like fopen(), fgets(), fscanf(), etc to write to the file. Instead, we can only use Unix system calls like open(), read(), write(), and close(). However, I would like to make sure that my output file has not been opened by another application before writing to it. is there any facility to do this in C?

Upvotes: 0

Views: 293

Answers (1)

Rich
Rich

Reputation: 650

Simply use a pseudo-random file name and open the file with (O_CREATE | O_EXCL) as options. If the file already exists, the open will fail with EEXIST and you can try again with a new name.

Note that this doesn't guarantee anything, just reduces the possibility. A true guarantee will require filesystem and OS specific methods.

Upvotes: 1

Related Questions