fs::recursive_directory_iterator exception unhandled

792 Views Asked by At

I wrote code that searchs ".data" files in directory, when i do it in this way, works perfectly:

std::string path("C:\\Users\\MyPC\\OneDrive\\Desktop\\Database");
    std::string ext(".data");
    std::string Datas[50];
    int DataCounter = 0;

    for (auto& p : fs::recursive_directory_iterator(path))
    {
        if (p.path().extension() == ext) {
            Datas[DataCounter] = p.path().stem().string();
            ++DataCounter;
        }
    }

but when I input same directory from using std::cin, it throw unhandled exception error. Code like this:

std::string path("C:\\Users\\MyPC\\OneDrive\\Desktop\\Database");
std::string ext(".data");
std::string Datas[50];
int DataCounter = 0;
SetColor(2);
std::cout << "Please type a path to search database: ";
SetColor(7);
std::cin >> path; //Entered same path. C:\\Users\\MyPC\\OneDrive\\Desktop\\Database

for (auto& p : fs::recursive_directory_iterator(path))
{                 //^ exception thrown here
    if (p.path().extension() == ext) {
        Datas[DataCounter] = p.path().stem().string();
        ++DataCounter;
    }
}

Error message is this:

Unhandled exception at 0x764640B2 in Interpol Database.exe: Microsoft C++ exception: std::filesystem::filesystem_error at memory location 0x00AFE4B8.

1

There are 1 best solutions below

0
On

Interestingly; std::cin.ignore() Solves the problem.