lundi 19 juillet 2021

Can't open file to read data

I am using VS2019 and the following simple snippet of code doesn't work properly (it can't open a given file):

#include <iostream>
#include <fstream>

int main(int argc, char** argv)
{
    std::ifstream inFile("Ids.txt");

    try {
        inFile.exceptions(inFile.failbit);
    }
    catch (const std::ios_base::failure& e)
    {
        std::cout << "Caught an ios_base::failure.\n"
            << "Explanatory string: " << e.what() << '\n'
            << "Error code: " << e.code() << '\n';
    }

    if (!inFile.is_open()) {
        std::cout << "Could not open a file!\n";
    }

    return 0;
}

I have placed Ids.txt file wherever executable is, but I am getting the following output after running a program:

Caught an ios_base::failure. Explanatory string: ios_base::failbit set: iostream stream error Error code: iostream:1 Could not open a file!

The file just contains 3 integers written line by line, but I guess it doesn't even matter, because it just can't open given file.

Aucun commentaire:

Enregistrer un commentaire