I'm making a simple text game where you kill enemies, level up, and get weapons. The levels of the enemies are based on your level, and so are the weapons you get and the damage you do. I would like to create a very basic save file system for it.
My idea is that before the game starts, it will look for the save file which is on the desktop, and check if it's "good". If it can't find the save file, or if the save file isn't "good", it should create the save file. What I have so far runs without errors, but says that the file is bad every time and does not create it.
Lines 27 ad 31 do not do anything, as you are using the equality operator (==) instead of assignment (=). In any case, those sets of if statements seem a bit redundant. Why check good then conditionally assign to a separate boolean variable? Why not simply do the fileexists == true code at line 27, etc.?
Also, an ofstream is going be good in most all cases. I think the only time it wouldn't be is if the file is in use already. If it doesnt exist, it will just make one. You could open the file in append mode, and see if you are at a file position other than zero, or also try opening the file as an ifstream, as these will fail when the file doesnt exist.
JLBorges, that code doesn't work either. It gives me a error on code::blocks. Keep in mind that I'm making this for the Mac OS, and it needs to be able to create the text file if it doesn't exist. I really need help with this.
The IDE is not the compiler. g++ or clang wouldn't give an error.
> Keep in mind that I'm making this for the Mac OS
In this case, the OS an irrelevant distraction; except that Mac OS is Unix, and so path2file must be a POSIX path.
> it needs to be able to create the text file if it doesn't exist.
1 2 3 4
// Append to an existing file (or create a new file) for writing:
// If a file with the same name already exists append data at the end of the file.
// if it does not exist, create a new empty file.
std::ofstream file( "path2file", std::iosbase::app ) ;