I have a simple example:
#include <stdio.h>
#include <errno.h>
int main() {
int result = rename("filea", "doesntexist/fileb");
if (result != 0) {
printf("NOOOO %d\n", errno);
}
return 0;
}
and I want to distinguish between 2 of the possible failures:
- filea doesn’t exist
- directory for fileb doesn’t exist
but it always returns errno = 2 when either doesn’t exist… uhm
Any ideas how can I approach this?
Thanks
EDIT: If possible without manually checking if the files exist.
EDIT2: Not checking if the file exists is a stupid constraint 😉 so, I’ve accepted one of the answers already. Thanks!
I don’t know how you’re going to check if a file exists without checking if a file exists, but hopefully this function will help you out:
If your goal is to keep the code clean, then just use functions:
You could return custom error codes from
renameFiles()and conditionally handle errors based on which file does or does not exist, or if there is some other problem with therename()call.