Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31781
  • 博文数量: 9
  • 博客积分: 65
  • 博客等级: 民兵
  • 技术积分: 55
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 10:36
文章分类

全部博文(9)

文章存档

2011年(9)

分类:

2011-09-23 15:25:34

1.获取当前工作目录
      #include <unistd.h>
  1. char *getcwd(char *buf,size_t size);

  2. 其中,buf为缓冲区地址,size为给出的最大路径名长度。如果当前工作目录的路径名长度大于给定的长度,则返回NULL并置errno为ERANGE。函数调用成功时,返回指向路径名的指针;否则返回NULL

  3. 例:
  4. char *name = new char[256];
  5. if(getcwd(name,255)!=NULL)
  6.     cout<<"current dir is: "<<name<<endl;
  7. delete [] name;
2. stat结构体
 
  1. #include <sys/stat.h>

  2. struct stat
  3. {
  4.   mode_t st_mode;       //文件类型和权限信息
  5.   ino_t st_ino;         //i结点标识
  6.   dev_t st_dev;         //device number (file system)
  7.   dev_t st_rdev;        //device number for special files
  8.   nlink_t st_nlink;     //符号链接数
  9.   uid_t st_uid;         //用户ID
  10.   gid_t st_gid;         //组ID
  11.   off_t st_size;        //size in bytes,for regular files
  12.   time_t st_st_atime;   //最后一次访问的时间
  13.   time_t st_mtime;      //文件内容最后一次被更改的时间
  14.   time_t st_ctime;      //文件结构最后一次被更改的时间
  15.   blksize_t st_blksize; //best I/O block size
  16.   blkcnt_t st_blocks;   //number of disk blocks allocated
  17. }
  1. 获取文件stat结构体信息的三个函数

  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>

  5. int stat(const char *path, struct stat *buf);
  6. int fstat(int fieldes, struct stat *buf);
  7. int lstat(const char *path, struct stat *buf);

  8. 成功则返回0,否则返回-1.
  9. 其中,fstat的第一个参数int fields为文件的描述符。
  10. fstat区别于另外两个系统调用的地方在于,fstat系统调用接受的是 一个“文件描述符”,而另外两个则直接接受“文件全路径”。文件描述符是需要我们用open系统调用后才能得到的,而文件全路径直接写就可以了。

  11. stat与lstat的区别:

  12. 当文件是一个符号链接时,lstat返回的是该符号链接本身的信息;而stat返回的是该链接指向的文件的信息。可以这样记,lstat比stat多了一个l,因此它是有本事处理符号链接文件(link)本身的。
3.dirent结构体,可以只关注d_name字段
 
  1. #include <dirent.h>

  2. struct dirent
  3. {
  4.   long d_ino;                /* inode number 索引节点号 */   
  5.   off_t d_off;               /* offset to this dirent 在目录文件中的偏移 */  
  6.   unsigned short d_reclen;   /* length of this d_name 文件名长 */  
  7.   unsigned char d_type;      /* the type of d_name 文件类型 */
  8.   char d_name [NAME_MAX 1];  /* file name (null-terminated) 文件名,最长255字符 */  
  9. }
 
4.读取目录的操作
 
  1. //可能用到的函数

  2. #include <sys/types.h>
  3. #include <dirent.h>

  4. DIR *opendir(const char *dirname);
  5. struct dirent *readir(DIR *dirp);
  6. void rewinddir(DIR *dirp);
  7. int closedir(DIR *dirp);

  8. //一个完整的递归遍历目录下子目录及文件的函数

  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <dirent.h>
  12. #include <string.h>
  13. #include <stdlib.h>

  14. void printdir(char *dir, int depth)
  15. {
  16.   DIR *dp;
  17.   struct direct *entry;
  18.   struct stat statbuf;

  19.   if( (dp = opendir(dir))!=NULL )
  20.   {
  21.     chdir(dir);         //changedir,进入dir目录
  22.     while( (entry = readdir(dp))!=NULL )
  23.     {
  24.       lstat(entry->d_name,&statbuf);

  25.       if( S_ISDIR(statbuf.st_mode) )
  26.       {
  27.         if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)
  28.           continue;

  29.         printf("%*s%s/\n",depth," ",entry->d_name);
  30.         printdir(entry->d_name,depth+4);
  31.       }
  32.       else
  33.         printf("%*s%s\n",depth," ",entry->d_name);
  34.     }

  35.     chdir("..");
  36.     closedir(dp);
  37.   }
  38.   else
  39.   {
  40.     fprintf(stderr,"cannot open directory: %s\n",dir);
  41.     return;
  42.   }
  43. }

  44. int main()
  45. {
  46.   printf("Directory scan of /home:\n");
  47.   printdir("/home",0);
  48.   printf("Success!\n");

  49.   exit(0);
  50. }
 
 5.注意:判断文件是目录文件还是普通文件有两种方式
   
  1. struct dirent *entry;
  2. struct stat st;

  3. entry = opendir(dir);
  4. lstat(entry->d_name,&st);
  5. S_ISDIR(st.st_mode); //是目录文件则函数返回true
  6. S_ISREG(st.st_mode); //是普通文件则函数返回true

  7. //S_ISDIR和S_ISREG均为宏定义,可参见sys/stat.h.
  8. //或者用以下方式
  9. (st.st_mode & S_IFMT)==S_IFDIR //目录文件为true
  10. (st.st_mode & S_IFMT)==S_IFREG //普通文件为true
  11. //其中S_IFMT在sys/stat.h中被定义为八进制数170000,通过与st_mode进行位的与操作可得类型,另有S_IFBLK、S_IFCHR、S_IFIFO类型
 
阅读(1082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~