• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Best code to check if a file is present

## Recommended Posts

I don''t want to rely on return code of Dx8 loadtexture for some reason and also while in debug mode it return some "file is missing" in the debug windows wich will let me miss some real file missing... For now I use:
  BOOL CheckFile(char* Filename) { FILE * stream; stream = fopen(Filename, "rb" ); if(stream==NULL) return FALSE; fclose( stream ); return TRUE } 
Is there any better method ? faster, more reliable ? any downside with the previous method ? Thanks Dan

##### Share on other sites
Your way works and is quite fast. The only better way I can think of off the top is if you are using C++ Builder. If so, just do this: "if (FileExists (FileName))". That will be slightly faster and is also slightly cleaner.

##### Share on other sites
Your way works and is quite fast. The only better way I can think of off the top is if you are using C++ Builder. If so, just do this: "if (FileExists (FileName))". That will be slightly faster and is also slightly cleaner.

##### Share on other sites

CheckFile() doesn''t work all the time. For example, a
file exists but you don''t have read permission.

Premature optimizations can only slow down your project even more.

##### Share on other sites
What do you mean fast? You mean small amount of code right? I mean how much faster or slower can it be. You meant speed in regard to programming and not in regard to program execution right?

##### Share on other sites
What do you mean fast? You mean small amount of code right? I mean how much faster or slower can it be. You meant speed in regard to programming and not in regard to program execution right?

##### Share on other sites

I will then continue to use it (I don''t have C++ builder)

Dan

• ### Forum Statistics

• Total Topics
627743
• Total Posts
2978892

• 10
• 10
• 21
• 14
• 14