博客
关于我
unix环境编程 目录操作
阅读量:225 次
发布时间:2019-02-28

本文共 1170 字,大约阅读时间需要 3 分钟。

1、目录操作有 mkdir(constchar *pathname,mode_t mode)

创建目录,成功则返回0,若出错则返回-1

int rmdir(const char *pathname)

rmdir可以删除一个空目录,成功则返回0,若出错返回-1

DIR *opendir(const *pathname)

成功则返回指针,若出错则返回NULL

struct dirent *readdir(DIR *dp)

若成功则返回指针,出处则返回NULL

void rewinddir(DIR *dp)   成功则返回0,若出错则返回-1

int closedir(DIR *dp)   成功则返回0,若出错则返回-1

int chdir(const char *pathname)    切换目录

成功则返回0,若出错则返回-1

char *getcwd (char *buf,size_t size)  获取当前工作目录

若成功则返回buf,若出错则返回NULL

2.打印某个目录下的所有文件名

[cpp] 
  1. # include <dirent.h>  
  2. int main(int argc,char *argv[])  
  3. {  
  4. DIR *dir=NULL;  
  5. struct dirent *ptr=NULL;  
  6. if((dir=opendir(argv[1]))==NULL)  
  7. {  
  8. perror("opendir");  
  9. return -1;  
  10. }  
  11. while((ptr=readdir(dir))!=NULL)  
  12. {  
  13. printf("%s \n",ptr->d_name);  
  14. }  
  15. return 0;  
  16. }  
  17.   
  18. # include <stdio.h>  

3.文件通配符,程序功能类似于ls  *.txt , 打印满足条件的文件

程序中的popen()和pclose()函数会创建一个管道,调用fork(),产生子进程,子进程执行命令,父进程与子进程之间利用管道实现进程间通信。

[cpp] 
  1. # include <stdio.h>  
  2. # include <string.h>  
  3. # include <unistd.h>  
  4. int main(int argc,char **argv)  
  5. {  
  6. char buf[256];  
  7. int i=1;  
  8. strcpy(buf,"ls ");  
  9. for(;i<argc;i++)  
  10. {  
  11.  strcat(buf,argv[i]);  
  12. strcat(buf," ");  
  13. }  
  14.  FILE *f=NULL;  
  15. if((f=popen(buf,"r"))==NULL)  
  16. {  
  17. perror("popen");  
  18. return -1;  
  19. }  
  20. while(fgets(buf,sizeof(buf),f))  
  21. printf("%s",buf);  
  22. pclose(f);  
  23. return 0;  
  24. }  

转载地址:http://lmwp.baihongyu.com/

你可能感兴趣的文章
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用exec节点实现调用外部exe程序
查看>>
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用node-red-contrib-image-output节点实现图片预览
查看>>
Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中使用range范围节点实现从一个范围对应至另一个范围
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node-RED中将CSV数据写入txt文件并从文件中读取解析数据
查看>>
Node-RED中建立TCP服务端和客户端
查看>>