Solved

Check to see if a file exists with C++

Posted on 2014-09-25
3
635 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

776 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