I need help writing certain lines of code

Sep 30, 2013 at 9:52pm
I need help writing a specific line of code:

1) I'm asking use to enter a file name. How do I write an error checking if/else statement that checks if the file name exists withing the folder?
Like lets say the file is name file.txt. If the user enters an invalid file such as fil.txt I want the program to exit

1
2
3
4
cout << "Please enter the name of the file that needs to be encrypted using the rot method:"<<endl;
      cin >> fileName;
 ifstream fin;
  fin.open(fileName.data());


Sep 30, 2013 at 9:55pm
http://www.cplusplus.com/reference/fstream/ifstream/is_open/

This will of course only indicate whether the file is open, a file may not have been able to be opened for more reasons than not existing, but this is close enough. You could also use boost to do it, which can tell you more correctly if the file exists or not, though I think mainly your concern will be whether you can open it or not (why would you want to handle the case that it exists but can't be opened, for example?)
Last edited on Sep 30, 2013 at 9:55pm
Sep 30, 2013 at 10:32pm
So mine would look like

if(fileName.is_open())

?
Sep 30, 2013 at 11:30pm
I tried that and i'm getting the error now

82: error: âstruct std::stringâ has no member named âis_openâ
Sep 30, 2013 at 11:37pm
you need to check fin.is_open()
Oct 1, 2013 at 12:21am
i used if(!fin)

thanks though
Topic archived. No new replies allowed.