1.获取当前工作目录
#include <unistd.h>
1.char *getcwd(char *buf,size_t size);
2.
3.其中,buf为缓冲区地址,size为给出的最大路径名长度。如果当前工作目录的路径名长度大于给定的长度,则返回NULL并置errno为ERANGE。函数调用成功时,返回指向路径名的指针;否则返回NULL
4.
5.例:
6.char *name = new char[256];
7.if(getcwd(name,255)!=NULL)
8.    cout<<"current dir is: "<<name<<endl;
9.delete [] name;
2. stat结构体
 
1.#include <sys/stat.h>
2.
3.struct stat
4.{
5.  mode_t st_mode;       //文件类型和权限信息
6.  ino_t st_ino;         //i结点标识
7.  dev_t st_dev;         //device number (file system)
8.  dev_t st_rdev;        //device number for special files
9.  nlink_t st_nlink;     //符号链接数
10.  uid_t st_uid;         //用户ID
11.  gid_t st_gid;         //组ID
12.  off_t st_size;        //size in bytes,for regular files
13.  time_t st_st_atime;   //最后一次访问的时间
14.  time_t st_mtime;      //文件内容最后一次被更改的时间
15.  time_t st_ctime;      //文件结构最后一次被更改的时间
16.  blksize_t st_blksize; //best I/O block size
17.  blkcnt_t st_blocks;   //number of disk blocks allocated
18.}
1.获取文件stat结构体信息的三个函数
2.
3.#include <sys/types.h>
4.#include <sys/stat.h>
5.#include <unistd.h>
6.
7.int stat(const char *path, struct stat *buf);
8.int fstat(int fieldes, struct stat *buf);
9.int lstat(const char *path, struct stat *buf);
10.
11.成功则返回0,否则返回-1.
12.
13.其中,fstat的第一个参数int fields为文件的描述符。
14.fstat区别于另外两个系统调用的地方在于,fstat系统调用接受的是 一个“文件描述符”,而另外两个则直接接受“文件全路径”。文件描述符是需要我们用open系统调用后才能得到的,而文件全路径直接写就可以了。
15.
16.stat与lstat的区别:
17.
18.当文件是一个符号链接时,lstat返回的是该符号链接本身的信息;而stat返回的是该链接指向的文件的信息。可以这样记,lstat比stat多了一个l,因此它是有本事处理符号链接文件(link)本身的。
3.dirent结构体,可以只关注d_name字段
 
1.#include <dirent.h>
2.
3.struct dirent
4.{
5.  long d_ino;                /* inode number 索引节点号 */   
6.  off_t d_off;               /* offset to this dirent 在目录文件中的偏移 */  
7.  unsigned short d_reclen;   /* length of this d_name 文件名长 */  
8.  unsigned char d_type;      /* the type of d_name 文件类型 */
9.  char d_name [NAME_MAX 1];  /* file name (null-terminated) 文件名,最长255字符 */  
10.}
 
4.读取目录的操作
 
1.//可能用到的函数
2.
3.#include <sys/types.h>
4.#include <dirent.h>
5.
6.DIR *opendir(const char *dirname);
7.struct dirent *readir(DIR *dirp);
8.void rewinddir(DIR *dirp);
9.int closedir(DIR *dirp);
10.
11.
12.//一个完整的递归遍历目录下子目录及文件的函数
13.
14.#include <unistd.h>
15.#include <stdio.h>
16.#include <dirent.h>
17.#include <string.h>
18.#include <stdlib.h>
19.
20.void printdir(char *dir, int depth)
21.{
22.  DIR *dp;
23.  struct direct *entry;
24.  struct stat statbuf;
25.
26.  if( (dp = opendir(dir))!=NULL )
27.  {
28.    chdir(dir);         //changedir,进入dir目录
29.    while( (entry = readdir(dp))!=NULL )
30.    {
31.      lstat(entry->d_name,&statbuf);
32.
33.      if( S_ISDIR(statbuf.st_mode) )
34.      {
35.        if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)
36.          continue;
37.
38.        printf("%*s%s/\n",depth," ",entry->d_name);
39.        printdir(entry->d_name,depth+4);
40.      }
41.      else
42.        printf("%*s%s\n",depth," ",entry->d_name);
43.    }
44.
45.    chdir("..");
46.    closedir(dp);
47.  }
48.  else
49.  {
50.    fprintf(stderr,"cannot open directory: %s\n",dir);
51.    return;
52.  }
53.}
54.
55.int main()
56.{
57.  printf("Directory scan of /home:\n");
58.  printdir("/home",0);
59.  printf("Success!\n");
60.
61.  exit(0);
62.}
 
 5.注意:判断文件是目录文件还是普通文件有两种方式
   
1.struct dirent *entry;
2.struct stat st;
3.
4.entry = opendir(dir);
5.lstat(entry->d_name,&st);
6.S_ISDIR(st.st_mode); //是目录文件则函数返回true
7.S_ISREG(st.st_mode); //是普通文件则函数返回true
8.
9.//S_ISDIR和S_ISREG均为宏定义,可参见sys/stat.h.
10.//或者用以下方式
11.(st.st_mode & S_IFMT)==S_IFDIR //目录文件为true
12.(st.st_mode & S_IFMT)==S_IFREG //普通文件为true
13.//其中S_IFMT在sys/stat.h中被定义为八进制数170000,通过与st_mode进行位的与操作可得类型,另有S_IFBLK、S_IFCHR、S_IFIFO类型
 

转载原文:http://blog.chinaunix.net/uid-25829053-id-2853278.html

Linux下文件及目录的一些操作(附递归遍历目录源码)的更多相关文章

  1. linux下文件压缩与解压操作

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...

  2. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  3. linux下文件的复制、移动与删除

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp     命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)      ...

  4. 学习笔记12—linux下文件的复制、移动与删除

    查看centOS 版本     cat /etc/redhat-release 1,复制粘贴文件 cp  [选项]  源文件或目录  目标文件或目录 2,剪切粘贴文件 mv [选项]  源文件或目录 ...

  5. linux 下文件重命名/移动/复制命令(转)

    linux 下文件重命名/移动/复制命令(转) linux下重命名文件:使用mv命令就可以了, 例:要把名为:abc   重命名为:123 可以这样操作: 重命名:MV命令 1.进入你的文件目录,运行 ...

  6. (转)linux下文件删除的原理精华讲解(考试题答案系列)

    linux下文件删除的原理精华讲解(考试题答案系列) 说明:本文为老男孩linux培训某节课前考试试题及答案分享博文内容的一部分,也是独立成题的,你可以点下面地址查看全部的内容信息.http://ol ...

  7. linux下文件的打包和压缩

    文章来源:linux下文件的打包和压缩 目录 一.文件压缩的原理 二.linux常见的压缩指令 三.常用实例 1.tar命令 2.zip命令 3.gz命令 4.bz2命令 5.xz命令(必须分两步) ...

  8. Linux下文件的三种时间戳

    Linux下文件的三种时间标记 三种时间对应关系表 column column column 访问时间 Access atime 修改时间 Modify mtime 状态改动时间 Change cti ...

  9. linux下文件结束符

    linux下文件结束符,我试过了所有的linux,发现其文件的结束符都是以0a即LF结束的,这个是操作系统规定的,windows下是\r\n符结束,希望可以帮助大家. -------------转:来 ...

随机推荐

  1. css案例学习之class执行的顺序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. JavaEE Tutorials (21) - Java EE安全:高级主题

    21.1使用数字证书331 21.1.1创建服务器证书332 21.1.2向证书安全域增加用户334 21.1.3为GlassFish服务器使用一个不同的服务器证书33421.2认证机制335 21. ...

  3. CentOS安装与配置LNMP

    本文PDF文档下载:http://www.coderblog.cn/doc/Install_and_config_LNMP_under_CentOS.pdf 本文EPUB文档下载:http://www ...

  4. CSS实现页面背景自动切换功能

    From here:http://xiaomiya.iteye.com/blog/2047728 请看效果图: 完整代码如下: <!DOCTYPE HTML> <html> & ...

  5. [11-3] Gradient Boosting regression

    main idea:用adaboost类似的方法,选出g,然后选出步长 Gredient Boosting for regression: h控制方向,eta控制步长,需要对h的大小进行限制 对(x, ...

  6. Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

    解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...

  7. css3教程:弹性盒模型

    Css3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间.这与XUL(火狐使用的用户交互语言)相似,其它语言也使用相同的盒模型,如XAML .GladeX ...

  8. AngularJS 实战讲义笔记

    第一部分 快速上手 1.1 感受AngularJs四大核心特性(MVC, 模块化,指令系统,双向数据绑定)1.2 搭建自动化的前端开发,调试,测试环境 代码编辑工具 (sublime) 断点调试工具 ...

  9. 学习dijk最短路径中

    #include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #i ...

  10. jq方法

    DOM属性-获取和设置页面元素的DOM属性 .addClass()..attr()..prop()..hasClass()..html()..removeAttr()..removeClass().. ...