linux--access function and mkdir function

Keywords: Linux

access function and mkdir function

These two functions are used when creating a new folder to store data locally

        std::string file_path1 = "./image_data/front_middle/" + std::to_string(time_stamp_) + ".jpg";
        if(access(file_path1,0)==-1)  // this dir is not exist .
            if(mkdir(file_path1,0744)==-1)
                std::cout<<"The data folder create error!"<<std::endl<<file_path1<<std::endl;
access
#include<unistd.h>
int access(const char* pathname, int mode);

Pathname is the pathname + filename of the file (or just the pathname)

mode: Specifies the function of access. The values are as follows:

F? OK value is 0, judge whether the file exists

The value of X 〝 OK is 1, which determines the executable permission to the file

W ﹣ OK value is 2, judge whether there is write permission to the file

The R ﹣ OK value is 4, which determines whether the file has read permission

Note: the last three methods can be used or "|", such as w|ok|r|ok

Return value: success 0 (exists), failure - 1 (does not exist)

mkdir
#include <stdio.h>
int mkdir(const char *pathname, mode_t mode);

Pathname is the pathname of the file

mode: define the permissions of the new directory

Return value: Return - 1 if creation failed, return 0 if creation succeeded

  char daystr[20];
    timeval tv;
    gettimeofday(&tv,NULL);
    strftime(daystr,sizeof(daystr),"%Y-%m-%d",localtime(&tv.tv_sec));
    for(int i=0;i<20;i++) {
        if(daystr[i]==':')
            daystr[i]='-';
    }
    save_path_string = save_path_string + daystr + std::string("/");

    if(access(save_path_string.c_str(),0) == -1)
        if(mkdir(save_path_string.c_str(),0744) == -1)
            std::cout<<"The data folder create error!"<<std::endl<<save_path_string<<std::endl;


    char mytime[20];
    strftime(mytime,sizeof(mytime),"%Y-%m-%d-%T",localtime(&tv.tv_sec));
    for(int i=0;i<20;i++) {
        if(mytime[i]==':')
            mytime[i]='-';
    }

    save_path_string2 = save_path_string + std::string("/") + mytime + std::string("/");
    if(access(save_path_string2.c_str(),0) == -1)
        if(mkdir(save_path_string2.c_str(),0744) == -1)
            std::cout<<"The data folder create error!"<<std::endl<<save_path_string2<<std::endl;

Posted by jrd on Sun, 26 Apr 2020 08:13:57 -0700