Check whether a file exists

I

Ivan Vecerina

: How can I check whether a file exists?

You'll get the same answer here as in the C NG ;)

The portable way to check for this is to try to open
the file (you may use ifstream, or fopen as in C)
to check if it succeeds, then close it immediately.

Two main issues are:
- various operating systems may have various
restrictions on visibility or accessibility
of files within a given storage/directory.
- on a multitasking OS, a file that exists
at a given time may be deleted by the next
statement.

So in general, best is to just try to use the file,
and report the error at that point.


hth-Ivan
 
M

Moonlit

Hi,

On most platforms there is a function called fstat, stat and lstat that uses
a filename, or descriptor and lets you check for various stuff about a file
(I believe it returns
'-1' if a file doesn't exist) Available on linux, solaris, windows, dec and
probably many more.
--


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top