Jump to content
  • Advertisement
Sign in to follow this  
virtuacasper

quick fstream question

This topic is 5055 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

im just brushing up my c++ skills(if any) and i stumbled apon a problem can somebody please tell me why the function below validates a none existant file..? bool Load(const char *path) { ifstream ifile(path,ios::in|ios::nocreate); if(ifile.bad()) { if(ifile.is_open()){ ifile.close(); } return false; } ifile.close(); return true; } i call it via if(!Load("x.txt")) // x.txt doesnt exist { cout<<"error: file not found.\n"; }

Share this post


Link to post
Share on other sites
Advertisement
Failing to open a file doesn't set ios::bad flag, so your call to ifile.bad () returns false, even when the file is not open.

You basically want to check wheter you were able to open the file, for which there is a function called is_open.


if (!file.is_open ())
return false;


Oxyd

Share this post


Link to post
Share on other sites
[attention][attention] There is no ios::nocreate in standard IOStream. [attention][attention]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!