0

I know this is a frequently asked question, but most solutions I have found are from 6-10 years ago and don't seem to work.

As a part of the C program I am writing in Visual Studio, I need to find a function that is able to return a boolean value - whether or not a file with a given name exists (the file in question is located in the debug directory, which is why I am saying file name and not file path). I need to implement it using a library I am able to include in VS, hence using access() from the unistd.h library will not work. Also, it has to be a safe function. If there's a function in the WINAPI that does all of that - that would be best.

Thanks in advance for the help.

Guy
  • 155
  • 11
  • `GetFileAttributes` or `RtlDoesFileExists_U` – RbMm Jan 20 '21 at 19:49
  • Does this answer your question? [Fastest way to check if a file exist using standard C++/C++11/C?](https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c) – Werner Henze Jan 20 '21 at 19:49
  • 3
    The C way is `stat()`. The C++ way is [`std::filesystem::exists()`](https://en.cppreference.com/w/cpp/filesystem/exists). The Win32 API way is [`GetFileAttributes()`](https://devblogs.microsoft.com/oldnewthing/20071023-00/?p=24713) – Remy Lebeau Jan 20 '21 at 19:49
  • https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathfileexistsa – Werner Henze Jan 20 '21 at 19:51
  • 2
    @WernerHenze - `PathFileExists` simply call `GetFileAttributes` + require *shlwapi.dll* - obviously worse compare direct call to `GetFileAttributes`. – RbMm Jan 20 '21 at 19:54
  • I'd rather not use PathFileExists as it complicates my implementation. I did try to use GetFileAttributes but it doesn't seem to work for some reason. I trying using the second answer from here (answered by Zach Burlingame): https://stackoverflow.com/questions/3828835/how-can-we-check-if-a-file-exists-or-not-using-win32-program but when I try to use it I keep getting a FALSE result (as in the file doesn't exist, even when it does exist) – Guy Jan 20 '21 at 20:01
  • 2
    With [`_access()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess?view=msvc-160) from the MS runtime library. `#include ` – Weather Vane Jan 20 '21 at 20:06
  • 1
    Or with [_access_s()](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/access-s-waccess-s?view=msvc-160). – Weather Vane Jan 20 '21 at 20:13
  • 1
    *GetFileAttributes but it doesn't seem to work for some reason* in sense ? – RbMm Jan 20 '21 at 20:19
  • It seemed like GetFileAttributes didn't work due to me misplacing the file. Anyway, I'll be going with _access_s here due to its simplicity and safety (both it and _access() are very simple to use). Thanks everyone! – Guy Jan 20 '21 at 20:46
  • 1
    [Superstition: Why is GetFileAttributes the way old-timers test file existence?](https://devblogs.microsoft.com/oldnewthing/20071023-00/?p=24713) – IInspectable Jan 20 '21 at 22:33
  • 1
    *Is there a simple way to check if a file exists in C (Visual Studio)?* In general, you ***DON'T***. Check-then-do is a [TOCTOU bug](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use). Note the very example of a TOCTOU bug on that Wiki page is "check if file exists". For example, if you need to create a file only if it doesn't exist, you open the file in exclusive mode per your OS's supplied functionality. Nevermind the fact that any "check" action can't be the same act as whatever the "do" action is, so the check can't be valid anyway. – Andrew Henle Jan 21 '21 at 10:25

1 Answers1

1

PathFileExistsW should do the job. It takes the path of the file or directory, which you want to check the existence of, as the first argument. It returns BOOL (TRUE, if the file or directory exists, and FALSE, if it doesn't. You have to include shlwapi.h as header and link against Shlwapi.lib in order to use this function.

Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33