代码:
#include <unistd.h>
#include <stdio.h>
/**
* A example to check the file existence.
*
*/
int isFileExist(const char* filename) {
return access(filename, F_OK) == 0;
}
/**
* Usage: a.out filename
*
*/
int main(int argc, const char* argv[]) {
if (argc != 2) {
printf("Usage: a.out filename\n");
return 1;
}
if (isFileExist(argv[1])) {
printf("%s exists.\n", argv[1]);
} else {
printf("%s does not exist.\n", argv[1]);
}
return 0;
}
示例:
~/examples/cpp/access % ./a.out
Usage: a.out filename
~/examples/cpp/access % ./a.out a.out
a.out exists.