Given a String [sPath] that is supposed to represent a Path/Name [like c:\Folder\File.txt].
I need a way to determine if that file actually exists and return some kind of boolean value. Any ideas? Thanks,
Printable View
Given a String [sPath] that is supposed to represent a Path/Name [like c:\Folder\File.txt].
I need a way to determine if that file actually exists and return some kind of boolean value. Any ideas? Thanks,
Quote:
Originally Posted by Shaitan00
I'm not shure if this is correct and I don't know how to use Win32 API calls in C (Yet) so, here goes...
Cheers,Code:#include <stdio.h>
FILE *fp;
int FileExists;
fp = fopen("SomeFile.txt", "r");
if (fp == NULL){
FileExists = 0;
}
else{
FileExists = 1;
fclose(fp);
}
RyanJ
It's correct and works most of the time. It's not very efficient, though, and may report false negatives if:
1) The file exists but you're not allowed to access it.
2) The file exists but has been locked against reading by another application.
3) Any other reason why the file might exist but not be openable.
Boost.Filesystem provides the exists method, and the WinAPI provides the FileExists function.