Hello, so i need to check, if certain file exists, first i check if the file has no dot, then if the extension is .txt or .dat, and then i need to check if the provided .txt file exists,
all good until it gets into the check for the opened file. it keeps looping even when im providing the correct file name.
The txt file that should be correct is one named dataFile3.txt
thanks
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream fin;
while (true)
{
cout << "Enter file Name ";
string filename;
cin >> filename;
auto dot = filename.rfind('.');
if (dot != filename.npos)
{
auto substr = filename.substr(dot + 1);
if (substr == "dat" || substr == "txt")
{
fin.open(filename);
if (fin)
break;
cout << "error: the file does not exist\n";
continue;
}
}
cout << "error: the filename must end in .txt or .dat\n";
}
cout << "file is open\n";
}