Is there way in C on Linux to only write to a file if it already exists? In other words, the opposite of open(..., O_CREAT|O_EXCL)
.
Note that I don't want the existence check decoupled from the actual opening of the file (like calling stat()
beforehand) because that would be a race condition.
To do this I would try to open the file with O_WRONLY, if open() doesn't fail, the file exists. You can check errno to check the error.
I don't know any other way.
Check for ENOENT
while trying to open the file without the O_CREAT
flag.
ENOENT O_CREAT is not set and the named file does not exist; or O_CREAT is set and either the path prefix does not exist or the path argument points to an empty string.
If you can use fopen
, then:
file=fopen(some_sime, "r+");
if (file) fd=fileno(file);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.