Sign in to follow this  

some method to check if exist specific file

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this