c4k3ss
c4k3ss

Reputation: 23

How to make code detect if a file exists or not in C++ 17?

I am new to C++ and I want to make my first game. I want to make a simple savegame system for it but I am struggling to make it see if a savegame already exists or not. What I have for now

#include <iostream>
#include <time.h>
#include <windows.h>
#include <fstream>
using namespace std;
int main()
{
/*command goes here*/ {
cout<<"<System>: Welcome! Please register."<<endl;
cout<<"(TIP: Username and password can be anything. Both will be saved in your savegame [Savegame.txt] and you will be automatically logged in.)"<<endl;
Sleep(2000);
ofstream ("Savegame.txt");
ofstream fout;
fout<<"Username: ";
cin>>name;
cout<<name<<endl;
cout<<endl;
cout<<"Password: ";
cin>>password;
fout<<password<<endl;
}
else
{
    cout<<"<System>: Welcome back!"<<endl;
    ifstream fin;
    fin>>name;
    fin>>password;
}
return 0;
}

Please help I am trying to do this for a week :(

Upvotes: 1

Views: 1386

Answers (4)

linsock
linsock

Reputation: 310

As already pointed out, Filesystem Cpp official reference allows you to have the full range of operations available, for this particular instance:

bool exists( const std::filesystem::path& p );
bool exists( const std::filesystem::path& p, std::error_code& ec ) noexcept;

If you use std::filesystem::exists without error code, be careful to catch and eventual std::filesystem::filesystem_error (could happen trying to inspect files in removable devices or if special conditions applies).

bool existing_save = std::filesystem::exists("Savegame.txt");

What i think you may missing is to correctly configure Visual Studio to use the right C++ set, which can be done by clicking menu Project -> "your project name" Properties that will open a window in which you can edit General properties -> C++ Language Standard and set C++17 instead of the default C++14. You can see what to do in the image below, hoping this could help you:

C++17 option to set assuming you're using Visual Studio

Upvotes: 0

linsock
linsock

Reputation: 310

Eventually, if you want to target older versions of C++, you can open the file in read mode, then check if it's open or not.

If it's open, it exists and you have your answer, otherwise it may be non-existent or have some kind of error during opening.

If so, you can try to open it in another mode and verify if it fails again or not, to rule out or verify your guess.

At least, that's what i did before standard version 17 (from C++11 in this example constrained by raw string in path which can be removed simply escaping the path).

e.g.

    string path = R"(...<my path>...)";
    std::fstream fp;
    fp.open(path, std::fstream::in);
    if (fp.is_open()) {
        // file exists
        fp.close();
    }
    else {
        fp.open(path, std::fstream::out);
        if (fp.is_open()) {
            // file has been created
            fp.close();
        }
        else {
            // other error occurred
        }
    }

Upvotes: 1

use the exists method:

std::filesystem::exists

example:

 const bool exists = std::filesystem::is_regular_file("myfile.txt");

Upvotes: 0

lubgr
lubgr

Reputation: 38267

You need to #include <filesystem> and then use the appropriate function, e.g.

const bool exists = std::filesystem::exists("myfile.txt");

It might also make sense to narrow the query a bit, as in

const bool exists = std::filesystem::is_regular_file("myfile.txt");

The latter subsumes the existance of the file.

Upvotes: 2

Related Questions