I need help writing certain lines of code

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());


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
So mine would look like

if(fileName.is_open())

?
I tried that and i'm getting the error now

82: error: âstruct std::stringâ has no member named âis_openâ
you need to check fin.is_open()
i used if(!fin)

thanks though
Topic archived. No new replies allowed.