Skip to content

Instantly share code, notes, and snippets.

@xiaom
Created February 12, 2012 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaom/1806808 to your computer and use it in GitHub Desktop.
Save xiaom/1806808 to your computer and use it in GitHub Desktop.
check if a file exists
#include <unistd.h>
// Check whether file exist
// http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform
if( access( fname, F_OK ) != -1 ) {
// file exists
} else {
// file doesn't exist
}
/*
You can also use R_OK, W_OK, and X_OK in place of F_OK to check for read permission, write permission, and execute permission (respectively) rather than existence, and you can OR any of them together (i.e. check for both read and write permission using R_OK|W_OK)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment