Search code examples
c++filec++-clistreamreader

Check if file exists using StreamReader


I open this file to read data, but if file doesn't exist it throws error. So how can I check if file exists? In console project I can use ifstream, where I can check it by writing if(!file) {}, and StreamReader doesn't allow me to do this.

StreamReader^ data = gcnew StreamReader("data.txt");

Solution

  • You need to read the documentation.

    The exception thrown is FileNotFoundException if the file cannot be found.