c++ - Trying again and again to open a file while giving wrong path -


I have started working on the file using C ++ for the first time and I would like to write a correct program that Allows me to

  1. Open the path giving file;
  2. If it fails to open (showing what error does not make any difference), ask a new route;
  3. Working on the file;
  4. Close the file;

The only point I can not do is second I have already tried some way:

  do {cout & lt; & Lt; "Path:"; Cin & gt; & Gt; Path; F. Open (path, ios :: in); } While (f.fail ());  

But if I write the path of an uneven file and then the path to the file that I want to open, the program constantly asks me for a new route and never closes.

PS = Will it be different if I use a new file using ios :: out (for example: a file name / extension containing invalid characters) Want to use the situation to prevent errors in creating?

Clear flags if its failed

  f.open (path, IOS: in); Whereas (f.fail ()) {f.clear (); Std :: cout & lt; & Lt; "Wrong file path, re-enter"; Std :: CIN & gt; & Gt; Path; F. Open (path, ios :: in); }  

Comments