Checking if a text file exists, and if it doesn't creating it

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ofstream;
using std::ios_base;

bool fileexists;

int level = 1;

string weapon;

int main()
{
    cout << endl << "Checking for save file..." << endl;
    sleep(2);
    ofstream savefile;
    savefile.open("Users/calebcohen/Desktop/savefile.txt");
    if(savefile.good())
    {
        fileexists == true;
    }
    else if(savefile.bad())
    {
        fileexists == false;
    }

    if(fileexists == true)
    {
        cout << endl << "Savefile found, reading..." << endl;
        sleep(1);
    }
    else if(fileexists == false)
    {
        cout << endl << "Savefile not found, creating one on Desktop" << endl;
        savefile.open("Users/calebcohen/Desktop/savefile.txt", ios_base::out);
        cin.get();
    }


    cout << endl << endl << "Hello, and welcome to ClichedBluefish's text adventure!" << endl;
    cout << endl << "Level: " << level << endl << "Weapon: " << weapon << endl << endl << endl;

    return 0;
}
Last edited on
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.
Zhuge, I cleaned it up a little bit based on what you said, this is what I have now. The problem still persists unfortunately. I also forgot to mention my OS, I'm running Mac OS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ofstream;
using std::ios_base;
using std::ifstream;

bool fileexists = false;

int level = 1;

string weapon;

int main()
{
    cout << endl << "Checking for save file..." << endl;
    sleep(2);
    ofstream savefile;
    savefile.open("Users/calebcohen/Desktop/savefile.txt");
    if(savefile.good())
    {
        fileexists = true;
    }

    if(fileexists == true)
    {
        cout << endl << "Savefile found, reading..." << endl;
        sleep(1);
    }
    else if(fileexists == false)
    {
        cout << endl << "Savefile not found, creating one on Desktop" << endl;
        savefile.open("Users/calebcohen/Desktop/savefile.txt", ios_base::out);
        cin.get();
    }
    savefile.close();


    cout << endl << endl << "Hello, and welcome to ClichedBluefish's text adventure!" << endl;
    cout << endl << "Level: " << level << endl << "Weapon: " << weapon << endl << endl << endl;

    return 0;
}
Last edited on
Unfortunately this code still isn't working.
1
2
3
4
#include <fstream>

inline bool file_exists_and_can_be_read_from( const char* path2file )
{ return std::ifstream(path2file) ; }
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.
> It gives me a error on code::blocks.

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 ) ;  


Last edited on
Topic archived. No new replies allowed.