...
This creates issues when trying to write portable code, or when trying to implement alternative behavior.
Protect
...
Existing dest_file
If the desired behavior is to ensure that any file referenced by dest_file
is not erased or overwritten, POSIX programmers must implement additional safeguards.
...
Code Block | ||
---|---|---|
| ||
char const *src_file = /* ... */; char const *dest_file = /* ... */; if (rename(src_file, dest_file) != 0) { /* Handle Error */ } |
Erase
...
Existing dest_file
If the desired behavior is to ensure that any file referenced by dest_file
is erased by the rename()
operation, Windows programmers must write additional code.
...
A programmer that wants an application to behave the same on any C99 implementation must first determine what behavior to implement.
Compliant Solution (Erase
...
Existing dest_file
)
This compliant solution ensures that any file referenced by dest_file
is portably removed.
...
The return value of remove()
is deliberately not checked, because it is expected to fail in the case where the file does not exist. If the file exists but cannot be removed, the rename()
call will also fail and the error will be detected at that point.
Compliant Solution (Protect
...
Existing dest_file
)
This compliant solution only renames the file referenced by src_file
if the file referenced by dest_file
does not exist.
...