Add SDL_RWExist

Hello everyone, I need a method that will check if a file exists. Thank!

Now, i do this - SDL_RWFromFile and SDL_RWclose

1 Like

Also need SDL_RWDelete and SDL_RWRename and SDL_RWFindfirst / SDL_RWFindnext.

I very much agree about those, especially as the platform-specific alternatives use different character sets (e.g. UTF-8 in Linux and ANSI in Windows). I’d like SDL_RWseteof() too, for extending or truncating a file.

But I’m not convinced of the value of SDL_RWExist(). Any such function can only tell you whether the file existed then, not whether it still exists now, so you must be prepared for the file not to exist even if it said it did (and vice versa) which in practice means it isn’t of great benefit.

But I’m not convinced of the value of SDL_RWExist() . Any such function can only tell you whether the file existed then , not whether it still exists now , so you must be prepared for the file not to exist even if it said it did (and vice versa) which in practice means it isn’t of great benefit.

I don’t think that matters for many use cases - what kind of files do games care about that just disappear?
Though I’d suggest SDL_RWstat() instead of SDL_RWexist() - that way you’d also get information about the file size and the kind of file (file/directory/…).
And a simpler alternative to SDL_RWFindfirst()/next() might be something that just lists the files of a directory, like opendir()/readdir()/nextdir() on Unix - that way SDL wouldn’t need to emulate the pattern matching of FindFirstFile()

Personally I think you should always be prepared for files to “just disappear”, especially if they are on a shared server. Raymond Chen has this to say on the subject of testing for file existence.

Obviously the importance of this depends on how serious the consequences might be if SDL_RWExist() says the file exists but it turns out not to (or the reverse); at the very least the documentation would need to call out the risk.

The RW Interface is not only an abstraction around file io. I don’t think implementing generic file io functions in an API that is meant to operate on abstract data io streams it is a good idea.

Functions for stat, exist and so on should go next to these here (and are IMO a little overdue): https://wiki.libsdl.org/CategoryFilesystem

Still, they are not things that should operate on RW streams.

Then don’t use that function when writing a database or whatever…
But if you’re in a game and you just wanna check if a savegame already exists to warn the player of overwriting it, or to automatically generate a new filename, or to rename the old savegame (in case you wanna keep a few old quicksaves around), this is useful, and if savegames magically disappear while your game is running you have other problems than “oops, now I chose a different name even though the old file doesn’t exist anymore after all”.
(Of course it’s still a good idea to fail gracefully when trying to rename the old file and it doesn’t exist anymore)

True.