ResidentBiscuit wrote: |
---|
In other words, it will be in the same directory as the exe file. |
No, it might not be.
Since he is running the exe from his IDE, it is more than probable that the file is being created (or trying to be created) in a different directory. And Windows7 is pretty strict about file permissions.
On Windows, the proper place to save files is the user's Application Data folder.
The simplest way to get that folder is to use the APPDATA environment variable.
But you'll have to use a Windows API function to create your saved game data directory.
Here's a function to create everything for you, and an example of how to use it to open a file for writing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <string>
#include <cstdlib>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
std::string GameFileName( const std::string& filename )
{
const char* p_appdata = std::getenv( "APPDATA" );
if (!p_appdata) throw 1;
std::string appdata( p_appdata );
appdata.append( "/johnnys-game" );
CreateDirectoryA( appdata.c_str(), NULL );
appdata.append( "/" );
appdata.append( filename );
return appdata;
}
|
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <fstream>
int main()
{
try {
std::ofstream f( GameFileName( "SavedGame.txt" ).c_str() );
f << "Game progress for Johnny's game.\n";
}
catch (int)
{
std::cerr << "Fooey! Could not save game data!\n";
}
return 0;
}
|
On Windows7, this should create a file named:
C:\Users\johnny\AppData\Roaming\johnnys-game\SavedGame.txt
(Where
johnny is your username -- or the username of whoever is logged on.)
Use it the same way to open a file for reading, using a
std::ifstream instead. Just remember that you cannot open an ifstream on a file that has not already been created.
Hope this helps.
PS. Windows purists, I know about SHGetKnownFolderPath(). And I know about Boost Filesystem. Let's not overload poor OP just yet with a hobbyist game for his friends. The code I posted is fine and safe.