checking for file existence

I'm making files to store data for a game I'm making. Theres a box that is going to display all the profiles and you will be able to select one. so how would i check if any files exist when i dont know the name of the file(s)? like there could be like 3 profiles saved and I have to check for those. heres my code so far for making a file if none exists:

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 <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> PlayerNames;
    vector<bool> FileExists;
    string input;
    string SelectedProfile;
    string directory = "Profiles/";

    int HighScore = 0;


    ofstream file;
    
    //check for existing files here
    //if there are push_back the FileExist vector

    if(FileExists.size() == 0)
    {
        cout << "No profiles found. Enter your name:" << endl;
        cin >> input;

        PlayerNames.push_back(input);
        SelectedProfile = input + ".profile";
        directory = "Profiles/" + SelectedProfile; // the files are in a folder where the .exe is Profiles/someone.profile
    }

    file.open(directory.c_str());

    if(file.is_open())
    {
        file << HighScore << endl;
        file.close();
    }

    else
    {
        cout << "Can't open the file!" << endl;
    }

    return 0;
}


or should I have just one file with all the data on it and somehow be able to read the all the data from in it
Last edited on
You have to decide what a profile file is. If it's name has a certain convetion (like <id>.profile), then you can check if such files exist. You can check a file's info without opening it with stat().

should I have just one file with all the data on it
I don't think that's a good idea.
Well there just going to have a generic name like player1.profile for the name. in the file it will be like this

name
level
score

example

john
1
1000

im still confused how i would check the folder if there are any files. what library is stat from?
Last edited on
You need to traverse the directory to get the filenames. Exactly how you do that depends of the operating system.

stat() is from the standard C library.

See http://www.cplusplus.com/forum/unices/36457/#msg197211
I'm using windows. I see now that it is os dependent. all I want to do is look in a folder that i already know exists and the location. then look at all the files in that folder to open them for data. It still seems easier just to have one file with a predetermined name and just the player data on each line like this:

john 1 1000
bob 3 1550

and just getline each line until there is a white space then break up the data into variables when the user selects what profile they wish to load. this isn't being done in the console but I got that part covered.
Last edited on
Check out FindFirstFile/FindNextFile/FindClose
Topic archived. No new replies allowed.