Linux目录操作


 创建目录:
   #include<sys/types.h>
   #include<sys/stat.h>

   int mkdir(const char *pathname,mode_t mode);
   第一个参数是要创建目录的名字,第二个参数指定了目录访问权限,进程的umask值会影响到最终生成目录的权限。
   创建成功的时候返回0,失败返回-1.mkdir会在创建的目录同时建立"."和".."两个链接。

 

   删除目录:
    #include<unistd.h>

    int rmdir(const char *pathname);
    参数pathname指定要删除的目录的路径名,只有在目录为空的情况下调用才会成功。


    打开目录:
    #include<sys/types.h>
    #include<dirent.h>

    DIR *opendir(const char *dirname);
    opendir的参数dirname指定要打开目录的路径名,如果成功,opendir返回一个指向DIR类型的指针,返回指针将指向目录

    结构的第一项。调用失败时系统会返回一个空指针。


    关闭目录:
    #include<dirent.h>

    int closedir(DIR *dirptr);
    closedir函数关闭参数dirptr所指向的目录流,该目录流通常是opendir调用的返回值。

     读取目录:
     #include<sys/types.h>
     #include<dirent.h>

     struct dirent *readdir(DIR *dirptr);
     readdir接受的参数是目录流指针,通常是函数opendir的返回值。第一次调用readdir后目录的第一项将被返回到struct dirent
     结构中。完成后目录指针移动到目录的下一项。
      dirent类型结构在头文件<dirent.h>中定义,该结构包含以下成员:
      ino_t d_ino;   //inode number
       char d_name[];//filename,null terminated
       
     在连续的调用readdir后,目录指针将到达目录的尾部,再调用readdir将返回空指针。
     如果想再从头开始读取目录,可以采用rewinddir系统调用:
     #include<sys/types.h>
     #include<dirent.h>

     void rewinddir(DIR *dirptr);
     调用rewinddir之后,下一个readdir调用将返回dirptr所指的目录的第一项

loopingdir.c:类似ls命令

#include<stdio.h>
#include<dirent.h>
#include<errno.h>

int my_double_ls(const char *name)要打开目录的路径
{
    struct dirent *d;
    DIR *dp;

    /*open the directory and check for failure*/
    if((dp=opendir(name))==NULL)
    {
        perror("open dir");
        return (-1);
    }

    /*continue looping through the directory,
     * printing out the directory entry name as long
     * as the inode number is valid
     */
    while(d=readdir(dp))遍历目录    {
/*        if(d->d_ino!=0)
        {
            printf("%s\n",d->d_name);
        }
*/
        printf("%s\n",d->d_name);
    }

    /*now go back to the beginning of the directory*/
    rewinddir(dp);//将目录指针指向目录的第一项

    /*print out the directory again*/
    while(d=readdir(dp))
    {
        printf("%s\n",d->d_name);
    }

    if(closedir(dp)==-1)
    {
        perror("closed dir");
        return (-1);
    }

    return 0;
}

int main(int argc,char **argv)
{
    if(argc!=2)
    {
        printf("The program must accept a commandline argument!");
        return (-1);
    }
    if(my_double_ls(argv[1])==-1)
    {
        perror("call my_double_ls failure!");
        return (-1);
    }
    return 0;
}



/*
  运行结果如下:
  jiang@jiangfeng:~/unixprog/2011-3-11$ ./loopingdir.o /home
.
jiang
..
lost+found
.
jiang
..
lost+found

*/


Linux目录操作例子:寻找特定目录文件


搜索特定目录找到以特定后缀结尾的文件或子目录。接受两个命令行参数:要搜索的目录名,后缀字符串。


#include<stdio.h> /*for NULL*/
#include<dirent.h>
#include<string.h>/*for string functions*/
#include<errno.h>

int match(const char *s1,const char *s2);

char *find_entry(char *dirname,char *suffix)
{
    char *p="no match file";
    static DIR *dp=NULL;
    struct dirent *d;

    
    
        if(dp!=NULL)
        {
            closedir(dp);
        }
        if((dp=opendir(dirname))==NULL)
        {
            perror("opendir");
     return (NULL);
        }
    

    while(d=readdir(dp))
    {
        if(d->d_ino==0)
        {
            continue;
        }
        if(match(d->d_name,suffix))
          {
          return (d->d_name);
       }
        else
        {
            return p;
        }

     }

}

/*字符串匹配时,返回1,不匹配返回0*/
int match(const char *s1,const char *s2)
{
  int diff=strlen(s1)-strlen(s2);//匹配字符串的后缀

    if(strlen(s1)>strlen(s2))
    {
      return (strcmp(&s1[diff],s2)==0);
    }
    else
        return 0;
}


int main(int argc,char **argv)
{
    char *p;
    if(argc!=3)
    {
        printf("commandline arguments error,only accept three arguments\n");
        return (-1);
    }
  if((p=find_entry(argv[1],argv[2]))==NULL)
    {
        perror("call find_entry error");
        return (-1);
    }
    printf("%s\n",p);
    
    return 0;
}

chdir系统调用:改变工作目录

使用chdir改变工作目录:
  #include<unistd.h>

  int chdir(const char *path);

  chdir调用使得调用其进程的当前工作目录变为path所指向的目录。改变当前工作目录,然后使用相对路径名访问文件要比使用绝对路径名效率更高。在失败的情况下返回-1

#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#define VERYBIG 200
void my_pwd(void);
int main(int argc,char **argv)
{
 if(argc!=2)
 {
  printf("only accept one argument!");
  return (-1);
 }
  
 printf("The olddir is :\n");
 my_pwd();
 chdir(argv[1]);
 printf("The newdir is:\n");
 my_pwd();
 return 0;
}
void my_pwd(void)
{
 char dirname [VERYBIG];
 if(getcwd(dirname,VERYBIG)==NULL)
 {
  perror("getcwd error");
 }
 else
 {
  printf("%s\n",dirname);
 }
}


getcwd系统调用 :得到当前目录

得到当前工作目录名:
  #include<unistd.h>

  char *getcwd(char *name,size_t size);
   getcwd返回一个指向当前工作目录路径名的指针,必须保证size参数要比最后返回的路径名长度大1。工作路径名也会保存在name参数中。


#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#define VERYBIG 200
void my_pwd(void);
int main(void)
{
 my_pwd();
 return 0;
}
void my_pwd(void)
{
 char dirname[VERYBIG];
        char *name;
 if((name=getcwd(dirname,VERYBIG))==NULL)
 {
  perror("getcwd error");
 }
 else
 {
 printf("%s\n",dirname);
 printf("%s\n",name);//getcwd返回的指针和接受的参数都是指向当前工作路径名的字符串
 }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值