Solved

Check to see if a file exists with C++

Posted on 2014-09-25
3
620 Views
Last Modified: 2014-09-28
Hi folks I need to check if a file exists in C++ but the solutions I found on the web didn't seem to work. Since I'm new at this any help with this would be great.
Cheers.
0
Comment
Question by:Rodric MacOliver
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40345034
You can do that using 'stat()' (http://linux.die.net/man/2/stat) or '_stat()' (http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) if you are on Windows, e.g.

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
   struct _stat buf;
   int result;
   char timebuf[26];
   char* filename = "crt_stat.c";
   errno_t err;

   // Get data associated with "crt_stat.c": 
   result = _stat( filename, &buf );

   // Check if statistics are valid: 
   if( result != 0 )
   {
      perror( "Problem getting information" );
      switch (errno)
      {
         case ENOENT:
           printf("File %s not found.\n", filename);
           break;
         case EINVAL:
           printf("Invalid parameter to _stat.\n");
           break;
         default:
           /* Should never be reached. */
           printf("Unexpected error in _stat.\n");
      }
   }
   else
   {
      // Output some of the statistics: 
      printf( "File size     : %ld\n", buf.st_size );
      printf( "Drive         : %c:\n", buf.st_dev + 'A' );
      err = ctime_s(timebuf, 26, &buf.st_mtime);
      if (err)
      {
         printf("Invalid arguments to ctime_s.");
         exit(1);
      }
      printf( "Time modified : %s", timebuf );
   }
}

Open in new window


(Code taken from the latter link)
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 100 total points
ID: 40345426
On Windows you can also try the PathFileExists() API
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773584(v=vs.85).aspx

If you are familiar with Boost which is my favourite then use the exists API
http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/tutorial.html#Using-status-queries
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 150 total points
ID: 40345521
or '_stat()' (http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) if you are on Windows,
to add to comment of jkr:

windows allows using function name 'stat' instead of '_stat' as well. so the following is portable between windows and linux.

#include <sys/stat.h>
bool FileExists(const char * szpath)
{
      struct stat fs;
      return stat(szpath, &fs) == 0;
}

Open in new window


Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now