Member Avatar for anu07

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

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 for anu07

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 for anu07

thank you very much jonsca,it worked :D

Be a part of the DaniWeb community

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