Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is a good practice to use access function

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

This is working with VS 2013

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

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

Is there a way to avoid having 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
like image 885
cristian Avatar asked May 25 '16 12:05

cristian


People also ask

What is the main function of Access?

Very simply, Microsoft Access is an information management tool that helps you store information for reference, reporting, and analysis. Microsoft Access helps you analyze large amounts of information, and manage related data more efficiently than Microsoft Excel or other spreadsheet applications.

When should I use Access over Excel?

In general, Access is better for managing data: helping you keep it organized, easy to search, and available to multiple simultaneous users. Excel is generally better for analyzing data: performing complex calculations, exploring possible outcomes, and producing high quality charts.

What are the four main functions of the Microsoft Access?

Ans. The four main objects of MS Access are tables, queries, forms, and reports.

What is Microsoft Access good for?

Microsoft Access is a popular information management tool that helps you store all kinds of information for reporting, analysis, and reference. With Microsoft Access, you can manage data more efficiently and analyze large amounts of information.


2 Answers

A solution that has no duplication, nor relies on a macro definition (besides the predefined one for platform detection) but has slightly more boilerplate than Aracthor's 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 fall back, because windows tends to be the exception more often than linux.

Another clean solution would be to define _CRT_NONSTDC_NO_WARNINGS and keep using the POSIX standard access in windows, without warnings about deprecation. As a bonus, this also disables warnings for using the standard strcpy instead of strcpy_s and similar. The latter is also standard (in C11), but optional and hardly any other C library implements them (and also, not all of the _s family functions in msvc comply to C11).

like image 180
eerorika Avatar answered Oct 05 '22 22:10

eerorika


There is another way, header-only solution.

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

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

It would include the right file on Linux systems and replace access with _access on other systems.

like image 30
Aracthor Avatar answered Oct 06 '22 00:10

Aracthor