Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This creates issues when trying to write portable code, or when trying to implement alternative behavior.

...

Preserve Existing Destination File

If the desired behavior is to ensure that the destination file is not erased or overwritten, POSIX programmers must implement additional safeguards.

...

Code Block
bgColor#ccccff
char const *src_file = /* ... */;
char const *dest_file = /* ... */;
if (rename(src_file, dest_file) != 0) {
  /* Handle Error */
}

...

Remove Existing Destination File

If the desired behavior is to ensure that the destination 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 (

...

Remove Existing Destination File)

This compliant solution ensures that any destination 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 (

...

Preserve Existing Destination File)

This compliant solution only renames the source file if the destination file does not exist.

...