Hey there,
I am making this game that reads maps from a text document, and there are about three txt's for each level. And so far, they are just all in the same folder as my .cbp codeblocks file. Or in my exe's folder. But I want to put them all in there own folder, so all the level 2 txt files will be in a folder called lvl2. How do i specify the lcoation? Because if i give my game to someone else, their path may be different to mine, so how can i write the location for the exe's folders it's running from, then another folder within that.?
Also, my text files are just full of numbers which are the coordinates for level's platforms. But if i try to read a char in there. The programm crashes. I am using c++, SDL and openGL.
This works fine:
You need to specify the fully qualified filename; that is, the path as well as the name.
If the path doesn't exist, you can create it. How you do that depends on the operating system or you can use a generic library (like Boost Filesystem) to do it generically.
You have two options:
1) Use relative path names
2) Use registry entries.
The second can be more difficult so lets look at the first option only. Try this out (This is windows only):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <fstream>
int main()
{
std::ofstream o1("test1.txt"); // Creates a file in the current directory
o1 << "complete";
std::ofstream o2("new/test2.txt"); // Creates a file in the "new" directory of the current directory
o2 << "complete";
std::ofstream o3("./test3.txt"); // Creates a file in the current directory
o3 << "complete";
std::ofstream o4("../test4.txt"); // Creates a file 1 directory up
o4 << "complete";
std::ofstream o5("../../test5.txt"); // Creates a file 2 directories up
o5 << "complete";
std::ofstream o6("../Parallel Folder/test6.txt"); // Creates a file in "parallel folder" 1 directory up
o6 << "complete";
}
If that doesn't work you could try extracting the current path yourself:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <string>
usingnamespace std;
int main (int argc, char** argv) {
string path = string(argv[0]);
int lastslash;
path = path.substr(0, path.rfind('\\'));
cout << path << endl;
}
For the last option, you'll need an installer for your program and you'll use this function: RegCreateKeyEx and RegSetValueEx. You'll store the path in a value defined by your new key.