How to know if a file exists?

K

Karim

Hi,

I need to write some code to figure out if a file exists before I
overwrite it.

I came up with this code, but not sure its the best..

bool isImgCached;
TFilePtr pCachedFile = fopen(cachedImgLoc.c_str(),"r");

if (pCachedFile)
{
printDebugInfo("Img is Cached .. using cached version"<<endl);
isImgCached = true;
fclose(pCachedFile);
}
else
{
isImgCached = false;
}

also to remove a file I am using std::remove(cachedImgLoc.c_str());

Any comments?

Thanks.
 
J

John Harrison

Karim said:
Hi,

I need to write some code to figure out if a file exists before I
overwrite it.

I came up with this code, but not sure its the best..

bool isImgCached;
TFilePtr pCachedFile = fopen(cachedImgLoc.c_str(),"r");

if (pCachedFile)
{
printDebugInfo("Img is Cached .. using cached version"<<endl);
isImgCached = true;
fclose(pCachedFile);
}
else
{
isImgCached = false;
}

also to remove a file I am using std::remove(cachedImgLoc.c_str());

Any comments?

Thanks.

I think your code is fine.

john
 
G

Gianni Mariani

Karim said:
Hi,

I need to write some code to figure out if a file exists before I
overwrite it.

I came up with this code, but not sure its the best..

bool isImgCached;
TFilePtr pCachedFile = fopen(cachedImgLoc.c_str(),"r");

if (pCachedFile)
{
printDebugInfo("Img is Cached .. using cached version"<<endl);
isImgCached = true;
fclose(pCachedFile);
}
else
{
isImgCached = false;
}

also to remove a file I am using std::remove(cachedImgLoc.c_str());

Any comments?

The only issue with this is that this will open the file. It will fail
if the file permissions are set so that the process trying to open the
file will cause the open to fail. Also, if the file is opened, then the
access time of the file is set, which is not always a good idea.

There is no standard C++ way to tell if a file exists. The best way it
to use create another interface that provides the information about a
file using the system dependent interfaces.

On Unix/linux the system call "access" will work and GetFileAttributes
for win32.

This however is not good enough all the time. Sometimes you want to
make sure that no other process creates the file in the meantime. For
this you need and "exclusive" open mechanism. Again standard C++ does
not provide an interface for this and so you need to implement your our
file interface. In Unix/Linux you need to create the file with O_EXCL
and the open system call will fail if the file exists on Win32 I think
there is a similar attribute to the CreateFile system call.

If you use the exclusive open mechanism you won't need the exist check
and you won't have any trouble with concurrent access to the file.
 
S

s5n

Hi,

I need to write some code to figure out if a file exists before I
overwrite it.

I came up with this code, but not sure its the best..

bool isImgCached;
TFilePtr pCachedFile = fopen(cachedImgLoc.c_str(),"r");

As someone mentioned before, this requires you to open the file, which
is overkill. Here's a snippet from one of my projects which does what
you want:

#if WIN32
# include <io.h>
# include <stdio.h>
#else
# include <unistd.h>
#endif

....

// static
bool path_finder::is_accessible( const std::string & path )
{
#if WIN32
# define CHECKACCESS _access
# define CHECKRIGHTS 0
#else
# define CHECKACCESS access
# define CHECKRIGHTS F_OK
#endif

return 0 == CHECKACCESS( path.c_str(), CHECKRIGHTS );
#undef CHECKACCESS
#undef CHECKRIGHTS
}


i hope that helps.

----- stephan
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top