some method to check if exist specific file

Started by
5 comments, last by Illco 18 years, 10 months ago
I found many code about loading texture file is not robust. it does not check if the texture file exist. so what are resolution ? I think first use a function to check if the file exist,then do load operation. what function can do this dectection operation?and are there some good resolution?
Advertisement
When using C or C++ you can do something like:
bool Exists( char* const pFilename ){    file* fpFile = fopen( pFilename, "rb" );  if ( !fpFile )    return false;  fclose( fpFile );  return true;}


However there is little gain. If the file does not exist your normal texutre loading routine will fail. If you check first if it exists, and it doesn't, the overall result will be quite the same (but now you know why the loading fails).
The only real solution is to actually try to read from the file, anything else will suffer from race conditions in a multitasking environment.
I agree with doynax. Are you using D3DXCreateTextureFromFile[Ex](), or your own method? If you're using your own method, you can just try to open the file and read from it. If opening fails, you know that the file doesn't exist, or you don't have permission to open the file (E.g. trying to open the swap file or an admin file under a non-admin user account). If reading fails, then the file is probably too short, or corrupt.

If you're using D3DXCreateTextureFromFile[Ex](), then you could try loading the file into memory and using D3DXCreateTextureFromFileInMemory[Ex]() instead.

I find it's pointless to tell the user exactly why the file couldn't be loaded (if the file doesn't exist, access denied, or file is invalid). If the app is for a technical person (A texture tool for example), they should know to check that the file exists and is valid, and if it's for the end user, they don't care; they just know that the file doesn't work.
I mean it for programer , if no the detection and then program will end with any useful information.

so not convenient to track program.
If this is under Win32, try the FindFirstFile() function.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:
The only real solution is to actually try to read from the file, anything else will suffer from race conditions in a multitasking environment.

Given that we are writing games here we can almost safely assume that this will not happen. If a game cannot assume that its native data will be there (in its install folders for example) all hope is lost of loading the content. If the files are not there, someone has intentionally fiddled with the game's native data and should be punished for it by the game not loading. In the case of a tool this will be different.

Quote:
If the app is for a technical person (A texture tool for example), they should know to check that the file exists and is valid, and if it's for the end user, they don't care; they just know that the file doesn't work.

With all due respect, I think is argument is at the very least doubtful. Technical persons also deserve good support and feedback from their application. Why would I or one of the modellers on our team bother to check if a file exists if the application (or tool) can do this for you? I think a message along the lines of "File does not exist, open something else" is way better than the app just crashing or the file not loading without explanation.

Greetz,

Illco

This topic is closed to new replies.

Advertisement