//#include<bits/stdc++.h> // <--- I do not have this file and you should not be using it either.
#include <iostream>
#include <Windows.h>
#include <fstream>
usingnamespace std;
int main()
{
ifstream in("test2.txt"); // <--- Defines stream variable and opens file for input.
while (!in.is_open()) // <--- Checks if not open. Could be simpley written as "(!in)". Does the same, but can catch more errors.
{
in.close();
ifstream in("test2.txt"); // <--- Defines stream variable and opens file for input, but in the scope of the while loop. This is different from the first "ifstream".
// Use "ofstream" if your intent is to create the file.
cout << "not" << endl; // <--- Since the while condition will always be true. This prints an endless loop to the screen.
//_sleep(2000); // <--- May not be available to everyone. The newer version is "Sleep()" in the <windows.h> file.
//Sleep(2000);
}
cout << " in "; // <--- Never reached because of the while loop.
return 0; // <--- Not required, but makes a good break point.
}
There is no test2.txt file when i run the program.
The program will make a loop to reload after 2 sec to check if the file exist then read it. I will add the test2.txt later.
If the file is exist, the program will do the next code.