0

I want to check the file name given as argv[1] and make sure that it is a file that actually exists in the folder. For simplicity sake, I have the below program to output all the lines in the file, but I would like to check that the file exists before doing that.

int main(int argc, const char *argv[]) 
{ 
    ifstream settings(argv[1]);
    string s = "";

    while (settings)
    {
        getline(settings, s);
        cout << s << endl;
    }
}
Hrolfr
  • 1
  • 3
  • 9
    Are you looking for [std::filesystem::exists](https://en.cppreference.com/w/cpp/filesystem/exists)? – Nathan Pierson Feb 26 '23 at 18:19
  • `if (!settings) /* file doesn't exist or can't be opened for some other reason */` – Pete Becker Feb 26 '23 at 18:33
  • Your goal is to read information from the file. The fact that the file exists does not guarantee the success of your operation. Attempt to read and handle the errors. There are multiple causes when the read fails, a non-existing file is only one of them. Maybe the file exists but you do not have permissions to read it. – axiac Feb 26 '23 at 19:05

0 Answers0