?
Solved

Check to see if a file exists with C++

Posted on 2014-09-25
3
Medium Priority
?
736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 400 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 35

Assisted Solution

by:sarabande
sarabande earned 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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