Jump to content
  • Advertisement

Archived

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

RegularKid

HELP...

This topic is 6071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement

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!

Share this post


Link to post
Share on other sites
  

ifstream fileInput("filename.ext");

if (fileInput)
{

// successful openning


}
else
{

// error (file not there or there was an error opening it


}



Hope this helps...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!