ifstream with wrong filename

Hi, another short question.

What happens if I call ifstream to process data from a file given by the string filename, but this file does not exist?
I assumed I would get an error, but it looks like my program will still run and only ignore the part where I am reading and processing data.

 
ifstream ist {filename};
Last edited on
You can check if the file is open with if (!ist) //file is not open . Reading from or writing to an unopened std::fstream will fail silently, which is why it's important to always check the status of the stream.
Last edited on
If you want an exception to be thrown as soon as an error occur you can enable this sort of behaviour using the exceptions function.

1
2
std::ifstream ist {filename};
ist.exceptions(std::ifstream::failbit);
Last edited on
Thank you guys!
Topic archived. No new replies allowed.