I am working on a save file system for a game, and trying to make sure I can get the file open in a small test program, determine whether it is empty, if it is empty prompt the user to create a character, and if its not empty load the character’s information into variables to be used for the game. So far in my testing I’ve created the files in notepad (leaving them empty), saved them with appropriate extensions, and attempted to open the file and test if they are empty.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ( int argc, char* argv[])
{
struct charFile
{
int chapterNum;
int savePoint;
string firstName;
char gender, hairColor, hairType, hairLength, eyeColor, profession, magic, martialSkills;
bool hasPet;
} character;
fstream save;
char saveFileChoice;
string saveFile;
cout << "Select a File (1, 2, 3, 4, 5, or 6): ";
cin >> saveFileChoice;
saveFile = saveFileChoice + ".charsav";
save.open(saveFile.c_str());
if (!save.good())
{
cout << "Save file cannot be opened.\n";
}
char tempStr[12];
save.getline (tempStr, 256);
if ( tempStr == "EMPTY" )
{
cout << "There is no save data in the file. Starting a new game...\n\n";
cout << "What is your character's name? ";
cin >> character.firstName;
save << character.firstName;
}
return 0;
}
I was wondering why the file was never dropping into the if statement even though it was empty, and then even when I added EMPTY ascii characters to the file and changed the condition. Then I put in:
if (!save.good())
{
cout << "Save file cannot be opened.\n";
}
and on running it constantly displays the message so for some reason the file isn’t opening. =/ I can’t figure out why though.
I’ve checked the files and they are not being formatted as .charsav.txt or anything, they are still just 1.charsav, 2.charsav, etc. I feel like I’m missing something easy and obvious. Can anybody point it out for me?
if (!strcmp(tempStr, “EMPTY” )) is how you compare strings