ubuntu下 C++ 函数创建目录

来源:互联网 发布:淘宝客自动转换工具 编辑:程序博客网 时间:2024/05/29 06:41

CreatDirectory 在windows环境,时windows 8.1 kits,不能在ubuntu系统中用cmake编译。

ubuntu 系统 采用mkdir创建目录。

注意: 


第一个参数类型到转化  如果是string类型,需要转化为 const char 类型。 转换方法如下:

1. string转const char*

   string s = "abc";

   const char* c_s =s.c_str();

2. const char*转string

   直接赋值即可

   const char* c_s ="abc";

   string s(c_s);

3. string转char*

   string s = "abc";

   char* c;

   const int len =s.length();

   c = new char[len+1];

   strcpy(c,s.c_str());

4. char*转string

   char* c = "abc";

   string s(c);

5. const char*转char*

   const char* cpc ="abc";

   char* pc = newchar[100];//足够长

   strcpy(pc,cpc);

6. char*转const char*

   直接赋值即可

   char* pc = "abc";

   const char* cpc = pc;




创建目录

    int mkdir(const char *pathname, mode_t mode);
    成功返回0,错误返回-1.

改变当前目录
    int chdir(const char *path);
    成功返回0,错误返回-1.

文件是否存在或是否可读可写
    int access(const char *pathname,int mode);
    pathname: 文件名
    mode: 以下的组合
    R_OK文件可以读
    W_OK文件可以写
    X_OK文件可以执行
    F_OK文件存在
    成功返回0,错误返回-1.

其他:
获得工作目录:#include char *getcwd(char *buf,size_t size);char *getwd(char *buf);
改变当前目录:#include int chdir(const char *path);
保存当前目录:#include int fchdir(int fd);
建立新目录:#include #include int mkdir(const char *path,mode_t mode);
删除目录:#include int rmdir(const char* path);
打开目录进行收索:#include #include DIR *opendir(const char *pathname); int dirfd(DIR *dirp);
关闭目录:#include #include int closedir(DIR *dirp);
搜索目录:#include #include struct dirent *readdir(DIR *dirp);
重新回到目录的开始:##include void rewinddir(DIR *dirp);
保存目录中的位置:#include #include long telldir(const DIR *dirp);
在目录内恢复位置:#include #include void seekdir(DIR *dirp,long loc);
扫描目录: #include #include int scandir(const char *diename,struct dirent ***namelist,int (*select)(struct dirent *),int (*compar)(const void *,const viod*));
遍历目录结构:#include int ftw(const char* path,int(*fn)(const char *obj_path,const struct stat *obj_stat,int obj_flags),int depth);
int nftw(const char* path,int(*fn)(const char *obj_path,const struct stat *obj_stat,int obj_flags,struct FTW obj_FTW),int depth,int flags);
改变根目录:#include int chroot(const char *dirname);



0 0
原创粉丝点击