博客
关于我
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/

你可能感兴趣的文章
mysql文本函数和数字函数
查看>>
Mysql新建用户和数据库并授权
查看>>
mysql日志
查看>>
mysql日志 事务问题_mysql因为事务日志问题无法启动
查看>>
mysql日志文件
查看>>
mysql日志管理学习笔记
查看>>
mysql日志问题定位实用命令
查看>>
MySQL日期时间函数大全
查看>>
mysql时间相减的问题
查看>>
mysql时间表示和计算
查看>>
MySQL是如何做容器测试的?
查看>>
mysql更改数据库表utf-8_修改mysql数据库为 utf-8
查看>>
mysql更改表引擎INNODB为MyISAM的方法总结
查看>>
mysql更新一个表里的字段等于另一个表某字段的值
查看>>
Mysql更新时间列只改日期为指定日期不更改时间
查看>>
MySQL更新锁(for update)摘要
查看>>
mysql更新频率_MySQL优化之如何了解SQL的执行频率
查看>>
mysql替换表的字段里面内容
查看>>
MySQL最多能有多少连接
查看>>
MySQL最大建议行数 2000w,靠谱吗?
查看>>