13.文件夹和目录操作
来源:互联网 发布:淘宝买家秀和卖家秀 编辑:程序博客网 时间:2024/06/04 19:56
目录的操作:
获取当前目录(getcwd)
char cwd[300];
getcwd(cwd,sizeof(cwd));
设置当前目录(chdir)
chdir(“/home”);
创建目录(mkdir)
mkdir(“test”,0751);
删除目录(rmdir,remove)
rmdir(“test”);
读取目录:
opendir,readdir,closedir,主要用于遍历目录。
#include<unistd.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<sys/types.h>#include<dirent.h>//列出某个目录下的文件列表以及相应 i 节点号,并且当遇到子目录,在其后面标示“(DIR)”字样。int main(){ DIR *dp; struct dirent *dirp; char dirname[]="./"; if((dp=opendir(dirname))==NULL){ perror("opendir error"); exit(1); } while((dirp=readdir(dp))!=NULL){ if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0)) continue; printf("%6d:%-19s %5s\n",dirp->d_ino,dirp->d_name,(dirp->d_type==DT_DIR)?("(DIR)"):("")); } return 0;}
文件夹遍历:
#include<unistd.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<sys/types.h>#include<dirent.h>#include <sys/stat.h>#include <unistd.h>//传入路径即可void show_all(const char* path){ DIR* dirp=opendir(path); //类似于FILE struct dirent* de=NULL; //dirent 获取文件夹目录属性 struct stat st; //stat 获取文件属性 char path2[500]; while(de=readdir(dirp)) { if(strcmp(de->d_name,".")==0 || strcmp(de->d_name,"..")==0) continue; printf("%s\n",de->d_name); strcpy(path2,path); strcat(path2,"/"); strcat(path2,de->d_name); stat(path2,&st); if(S_ISDIR(st.st_mode)) { show_all(path2); } } closedir(dirp);}
0 0
- 13.文件夹和目录操作
- 利用VB操作目录和文件夹
- 利用VB控件操作目录和文件夹
- 目录(文件夹)操作
- java目录文件夹操作
- Python 中针对文件和目录(文件夹)的常用操作
- 文件夹操作之----复制目录
- c#操作文件夹压缩到指定目录和解压到指定目录
- 目录和文件夹的区别
- shell编程----目录操作(文件夹)
- 目录操作/遍历文件夹中所有的文件及目录.
- 文件和目录操作
- 文件和目录操作
- 文件和目录操作
- 操作文件和目录
- 文件和目录操作
- 文件和目录操作
- 文件和目录操作
- 图片流量节省大杀器:基于腾讯云CDN的sharpP自适应图片技术实践
- Spring+SpringMVC+Hibernate框架整合
- 解决Android Studio 安装应用到手机上失败的问题
- Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
- CCF CSP试题 201612-2 工资计算
- 13.文件夹和目录操作
- javac/java命令行编译运行---小记
- 关于什么时候会发生死锁的问题
- 贪吃蛇初始画面
- NJCTF WEB Writeup
- Java开发手册
- Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
- GoLang之协程、channel、select、同步锁
- js判断页面加载完成绑定事件