So, I am looking into game development, and I just learned how save files work and how to create and implement them. Here is the code that I was using on Dev C++:
// basic file operations
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main () {
string line;
ifstream saveFiles ("save.txt");
if (saveFiles.is_open()) {
getline (saveFiles, line);
string arr[2];
int i = 0;
stringstream ssin(line);
while (i < 2){
ssin >> arr[i];
++i;
}
saveFiles.close();
cout << "You have " << arr[0] << " hearts" << endl;
cout << "You have " << arr[1] << " food";
}
else {
cout << "Hearts > ";
int hearts;
cin >> hearts;
cout << "Food > ";
int food;
cin >> food;
ofstream saveFile;
saveFile.open("save.txt");
saveFile << hearts << " " << food;
saveFile.close();
}
return 0;
}
For me, this checks if there is a save file, if there isn't, asks for input and then saves it. Then, when they come back, it checks again and it finds the file and gives you the data. This seems like it'd be how you make a save file (instead you likely wouldn't directly enter information like in my example).
As you can see, I create a save.txt
file and it works fine. However, for games that I've run (like Minecraft), it doesn't use a .txt
file. What difference does it make and what would work best for me (in any example)?