#### Archived

This topic is now archived and is closed to further replies.

# HELP...

## Recommended Posts

RegularKid    139
I am using fstream.h and am inputing from a file using ifstream. How do I check to see that the file already exists or not? - Thanks.

##### Share on other sites
Oluseyi    2116
fstream file;file.open(filename, ios::in|ios::nocreate);if(file.fail()){  // we either couldn''t open the file  // or couldn''t create it  return -1;}// use file here

You could also use file.good() to check if the open was successful (which means the file could be opened and thus exists) and close() it right after that.

I wanna work for Microsoft!

  ifstream fileInput("filename.ext");if (fileInput) { // successful openning }else { // error (file not there or there was an error opening it }