|
-
May 25th, 2005, 06:11 PM
#1
Thread Starter
Hyperactive Member
Determine if File Exists [C++6]
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,
-
May 25th, 2005, 06:58 PM
#2
Re: Determine if File Exists [C++6]
 Originally Posted by Shaitan00
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,
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...
Code:
#include <stdio.h>
FILE *fp;
int FileExists;
fp = fopen("SomeFile.txt", "r");
if (fp == NULL){
FileExists = 0;
}
else{
FileExists = 1;
fclose(fp);
}
Cheers,
RyanJ
-
May 26th, 2005, 04:36 AM
#3
Re: Determine if File Exists [C++6]
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|