Visual Studios Ignoring File

97 views Asked by At

Visual Studios is ignoring the file that I am trying to play. With this code it returns false since it doesn't load the file.

    result = LoadWaveFile("../Engine/data/sound01.wav", &m_secondaryBuffer1);
if (!result)
{
    return false;
}
1

There are 1 answers

0
Dialecticus On

Don't use relative path on current working directory. Construct instead an absolute path, combining GetModuleFileName and your original relative path. Current working directory is unreliable, and should not be used.

EDIT: What's wrong with current directories? In short it's volatile. First, the process may not start in the expected directory (like it was in your case). The user may create a shortcut to the app and specify any starting directory there. Second, there are functions in Windows API (notably GetOpenFileName) that can change the current directory as a side effect. Third, some third part library that the app uses can change current directory. Fourth, current directory is unique in the process, shared among threads, so even if you call SetCurrentDirectory before relying on it, some other thread can change it in the mean time.

In small demo app case, all those causes are almost non-existing, and you can even prevent the first cause by setting Project > Properties > Configuration Properties > Debugging > Working Directory to $(OutDir), if you are ever going to start the app from debugger. That's a very specific use case, though, so I would go with the more robust solution that does not rely on volatile nature of current directory.