Open directory using C

后端 未结 4 1331
时光取名叫无心
时光取名叫无心 2020-12-03 01:55

I am accepting the path through command line input.

When I do

dir=opendir(args[1]);

it doesn\' t enter the loop...i.e dir==nu

4条回答
  •  鱼传尺愫
    2020-12-03 02:11

    Here is a simple way to implement ls command using c. To run use for example ./xls /tmp

        #include
        #include 
        void main(int argc,char *argv[])
        {
       DIR *dir;
       struct dirent *dent;
       dir = opendir(argv[1]);   
    
       if(dir!=NULL)
          {
       while((dent=readdir(dir))!=NULL)
                        {
            if((strcmp(dent->d_name,".")==0 || strcmp(dent->d_name,"..")==0 || (*dent->d_name) == '.' ))
                {
                }
           else
                  {
            printf(dent->d_name);
            printf("\n");
                  }
                        }
           }
           close(dir);
         }
    

提交回复
热议问题