wtorek, 18 kwietnia 2017

class access file (linux)

#include <iostream>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

using namespace std;

class access_File
{
    private:
     char *my_path;
     int amode[3];
     int test_value;
    public:
     access_File(char *name)
     {
         my_path=name;
         amode[0]=F_OK;
         amode[1]=W_OK;
         amode[2]=R_OK;
     }
     void is_Exist()
     {
         test_value=access(my_path,amode[0]);
         if(test_value==0)
          cout<<my_path<<" exists"<<endl;
         else
         {
             if(errno==ENOENT)
              cout<<my_path<<" no exist"<<endl;
             else if(errno==EACCES)
              cout<<my_path<<" is no access"<<endl;
          }
     }
     void is_Write()
     {
         test_value=access(my_path,amode[1]);
         if(test_value==0)
          cout<<my_path<<" you can write "<<endl;
         else if (errno==EACCES)
          cout<<my_path<<" you can't write (not access) "<<endl;
         else if (errno==EROFS)
          cout<<my_path<<" you can't write (read-only system) "<<endl;
     }
     void is_Read()
     {
         test_value=access(my_path,amode[2]);
         if(test_value==0)
          cout<<my_path<<" you can read "<<endl;
         else
          cout<<my_path<<" tou can't read (not access) "<<endl;
     }
   
   
};

int main(int argc, char **argv)
{
    access_File File(argv[1]);
    File.is_Exist();
    File.is_Read();
    File.is_Write();
    return 0;
}

Brak komentarzy:

Prześlij komentarz