Check if a file exists using C++

There are many ways to check if a file exists in C++, however there are some ways which are better. Many people just say you should try to open it and if that fails, it doesn't exist. That method is slightly flawed because in some cases you may just not have permission to read the contents of the file, which is why the open is failing. A better way to check if a file exists is to use the stat() function. This function returns the file attributes of a file and therefore doesn't ever attempt to open it.

The following code is an example of how to use stat to check if a file exists. Note the comments which show you where this function could be extended if more details are required.

#include <sys/stat.h>

bool FileExists(string strFilename) {
  struct stat stFileInfo;
  bool blnReturn;
  int intStat;

  // Attempt to get the file attributes
  intStat = stat(strFilename.c_str(),&stFileInfo);
  if(intStat == 0) {
    // We were able to get the file attributes
    // so the file obviously exists.
    blnReturn = true;
  } else {
    // We were not able to get the file attributes.
    // This may mean that we don't have permission to
    // access the folder which contains this file. If you
    // need to do that level of checking, lookup the
    // return values of stat which will give you
    // more details on why stat failed.
    blnReturn = false;
  }
  
  return(blnReturn);
}

Using stat is also portable between operating systems and therefore you should be able to use this same code on Linux, Windows, Unix, Solaris, etc. The only thing that may differ between operating systems and compilers is the name of the include file (even though it shouldn't change).


Author: DPAK
Created: Dec 1 2005
Categories: C++
TechByte #103

Warning: By visiting this site and/or by using any information contained herein, you agree to the Techbytes.ca terms of use.


Comment posted by 'siby varghese' on May 2 2007 @ 19:05:06
thank you the this article
Comment posted by 'Anonymous' on Jul 16 2007 @ 19:03:33
Actually on Linux stat() happily returns a valid result for a directory. So it can return a false positive for, say, /tmp, which using this method returns 0 to indicate the "file" /tmp exists. D'oh.
Comment posted by 'hevean' on Nov 26 2007 @ 03:25:18
good
Comment posted by 'whigh' on Jan 28 2008 @ 16:13:26
Thank you. This is just what I was looking for. It works as nicely as expected.
Comment posted by '*****' on Feb 12 2008 @ 12:23:28
Good Article.
Comment posted by 'Boom' on Mar 2 2008 @ 21:03:52
wow, this actually works very well. I tried making one myself that opened the file, then checked if it was open, closing it afterward. This method kept messing up with other files, as I was using threads. However, this gets rid of all the problems.
Comment posted by 'goatslayer' on May 20 2008 @ 10:05:39
Neat method. Thanks for the tip.
Comment posted by ''Anonymous'' on Jun 2 2008 @ 20:03:54
Thanks very much! :)
Comment posted by 'biana' on Jun 13 2008 @ 08:43:00
It works well. Thanks you
Comment posted by 'Anonymous' on Jul 29 2008 @ 11:29:45
Works perfectly for me. Thanks a lot for your article!
Comment posted by 'Andy' on Aug 29 2008 @ 19:04:14
This very good

Add a comment about this TechByte

If you wish to add a comment regarding this TechByte, please use the form below. Please note that by submitting comments using this form you are allowing all of the information submitted to be visible on this website. Any comments submitted using this form will only be shown on the website if they are approved by the administrators of this site. IF APPROVED, COMMENTS MAY TAKE SEVERAL DAYS TO BE POSTED.

Posted By: (Optional)

Comments:


Other TechBytes: