Look up the access()
function, found in unistd.h
. You can replace your function with
if( access( fname, F_OK ) == 0 ) { // 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
)
Update: Note that on Windows, you can’t use W_OK
to reliably test for write permission, since the access function does not take DACLs into account. access( fname, W_OK )
may return 0 (success) because the file does not have the read-only attribute set, but you still may not have permission to write to the file.
Related Posts:
- What is Android’s file system?
- How to create a directory using Ansible
- A Simple, 2d cross-platform graphics library for c or c++?
- Cross Platform C library for GUI Apps?
- FreeBSD: Directory called ^C (really!) – how to remove?
- ZFS vs XFS
- warning: implicit declaration of function
- What is the difference between float and double?
- What is the difference between ++i and i++?
- Stack smashing detected
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- Openssl : error “self signed certificate in certificate chain”
- How do function pointers in C work?
- “Expected expression before ‘ { ‘ token”
- How to generate a random int in C?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- No space left on device
- What does “1e” mean?
- Getting “conflicting types for function” in C, why?
- how to use uint64_t in C [duplicate]
- How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?
- Warning comparison between pointer and integer
- What is use of c_str function In c++
- How do I properly compare strings in C?
- Connect: Socket operation on non-socket
- How to correctly use the extern keyword in C
- How do we check if a pointer is NULL pointer?
- how to use wait in C
- In C programming, what is `undefined reference`error, when compiling?
- Passing by reference in C
- double free or corruption (fasttop)
- error: expected declaration or statement at end of input in c
- Map like structure in C: use int and struct to determine a value
- Why does the arrow (->) operator in C exist?
- Why do I get clang: error: linker command failed with exit code 1?
- Should I learn C before learning C++?
- How to convert integer to char in C?
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- Program received signal SIGPIPE, Broken pipe
- How to read from stdin with fgets()?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Simple C program to read a file line by line
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- Does C have a “foreach” loop construct?
- What is a bus error? Is it different from a segmentation fault?
- Going through a text file line by line in C
- How to use glOrtho() in OpenGL?
- Reversing a string in C
- When a number is written as 0x00… what does the x mean
- How to remove the character at a given index from a string in C?
- waitpid, wnohang, wuntraced. How do I use these
- Convert C program into assembly code
- C: error: expected ‘)’ before ‘;’ token
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - Writing binary number system in C code
- Returning an array using C
- What are 0x01 and 0x80 representative of in C bitwise operations?
- Convert char array to string use C
- How to make parent wait for all child processes to finish?
- Use of cudamalloc(). Why the double pointer?
- OpenGL — GL_LINE_LOOP —
- Valgrind complains with “Invalid write of size 8”
- How to print an unsigned char in C?
- Read and write to binary files in C?
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- Difference between sizeof(char) and sizeof(char *)
- How to empty a char array?
- Undefined reference to main – collect2: ld returned 1 exit status
- What does ** mean in C?
- Is there a way to have printf() properly print out an array (of floats, say)?
- How do you allow spaces to be entered using scanf?
- How can I do GUI programming in C?
- Preventing console window from closing on Visual Studio C/C++ Console application
- What is the proper equivalent of “while(true)” in plain C?
- How do I compile the asm generated by GCC?
- What is the difference between %g and %f in C?
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- Structure padding and packing
- Implementation of strtok() function
- C fopen vs open
- switch case: error: case label does not reduce to an integer constant
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- warning: return makes pointer from integer without a cast but returns integer as desired
- Dynamic vs static array in c
- Suggestions needed: Effective Java to C source code converter
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- Implicit function declarations in C
- Convert long long to string in C?
- how to stop a loop arduino
- How to normalize a mantissa
- Compiler warning – suggest parentheses around assignment used as truth value
- LVM dangers and caveats