Member Avatar

Hi,can anyone tell me how to check if a file exists,I am using turbo c++ 3.0
Thank you for your time.

Recommended Answers

Yes, easiest is to open the file for read and check for an error.

Jump to Post

Well, you want to open it using ifstream, but since your compiler is pre-standard it may not have that method of the class (I seem to remember this from another poster at one point).

What will probably work (and is also used) is to test that the ifstream object …

Jump to Post

All 5 Replies

There may be some non-standard functions to do so in TC++, but I would just try to open it using an fstream object then use the .is_open() method to make sure that it has opened successfully. That's kind of quick and dirty, as I'm sure there are different OS dependent ways to do that, so someone should definitely correct me if I'm off base.

Yes, easiest is to open the file for read and check for an error.

Member Avatar

Okay I tried with .is_open(),but an error showed up

'is_open' is not a member of 'ofstream'
now what to do?

Well, you want to open it using ifstream, but since your compiler is pre-standard it may not have that method of the class (I seem to remember this from another poster at one point).

What will probably work (and is also used) is to test that the ifstream object is not null:

ifstream ifs("myfile.txt");
if(!ifs)
   //file didn't open for whatever reason
commented: worked :) +0
Member Avatar

thank you very much jonsca,it worked :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.