By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

How to know if a file exists?

P: n/a
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.

Feb 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Karim wrote:
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
Feb 18 '07 #2

P: n/a
Karim wrote:
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.
Feb 18 '07 #3

P: n/a
s5n
On Feb 18, 8:10 pm, "Karim" <karim.els...@gmail.comwrote:
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

Feb 18 '07 #4

P: n/a

I use stat (Unix/Linux) or _stat on Windows. Also if you are a boost
user:

http://www.boost.org/libs/filesystem...ons.htm#exists

:)

Karim wrote:
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.
Feb 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.