Solved

Check to see if a file exists with C++

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 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