Reputation: 2524
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
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