What is good practice to use the access function

I have the following code that I want to work on Linux with GCC 4.8

It works with VS 2013

if ( _access( trigger->c_str(), 0 ) != -1 ) 
{
   ...
}

I know that on Linux I can use the function: access from "unistd.h"

Is there a way to avoid something like the following (a more elegant solution)?

#ifdef __linux__ 
    #include <unistd.h>
#endif

#ifdef __linux__ 
     if ( access( trigger->c_str(), 0 ) != -1 ) 
     {
          ...
     }
#else
     if ( _access( trigger->c_str(), 0 ) != -1 )
     {
          ...
     }
#endif
+4
source share
2 answers

The solution, which has no duplication, and does not rely on the definition of a macro (other than the one predefined for platform discovery), but has slightly more templates than the Aracthor solution:

#ifdef _WIN32 
    inline int access(const char *pathname, int mode) {
        return _access(pathname, mode);
    }
#else
#include <unistd.h>
#endif

I prefer to detect windows and use posix as a rollback because windows tend to be the exception more often than linux.

_CRT_NONSTDC_NO_WARNINGS POSIX access . strcpy strcpy_s . ( C11), , C ( _s msvc C11).

+6

, .

#ifdef __linux__ 
    #include <unistd.h>
#else
    #define access _access
#endif

if ( access( trigger->c_str(), 0 ) != -1 ) 
{
      ...
}

Linux- access _access .

+3

All Articles