czwartek, 1 czerwca 2017

Two useful classes for linux (g++)

#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
using namespace std;


class copy_FILES
{
    private:
    struct stat st_value;
    int file_begin;
    int file_end;
    char *File1;
    char *File2;
    off_t off_value;
    public:
    copy_FILES(char *f1, char *f2)
    {
        off_value=0;
        File1=f1;
        File2=f2;
        file_begin=open(File1,O_RDONLY);
        fstat(file_begin,&st_value);
        file_end=open(File2,O_WRONLY | O_CREAT,st_value.st_mode);
        sendfile(file_end,file_begin,&off_value,st_value.st_size);
        close (file_begin);
        close (file_end);
    }
};
class show_Directory
{
    private:
    size_t path_size;
    DIR *directory;
    char *path_name;
    struct dirent *dirent_value;
    char path_max[PATH_MAX+1];
    public:
    show_Directory(char *path_directory)
    {
        char i='/';
        char j='\0';
       
        path_name=path_directory;
        strncpy(path_max,path_name,sizeof(path_max));
        path_size=strlen(path_name);
        if(path_max[path_size-1]!=i)
        {
            path_max[path_size]=i;
            path_max[path_size+1]=j;
            ++path_size;
        }
    }
    void Write()
    {
        directory=opendir(path_name);
        while((dirent_value=readdir(directory))!=NULL)
        {
            strncpy(path_max+path_size,dirent_value->d_name,
            sizeof(path_max)-path_size);
            cout<<path_max<<endl;
        }
        closedir(directory);
    }
};
int main(int argc, char **argv)
{
   
    show_Directory sD("/proc/");
    sD.Write();
    copy_FILES cF("/proc/cpuinfo","my_new_file_cpu");
    return 0;
}

Brak komentarzy:

Prześlij komentarz