鱼C论坛

 找回密码
 立即注册
查看: 3281|回复: 2

[技术交流] linux下边自己写的一个列出指定目录的c文件

[复制链接]
发表于 2011-8-20 17:32:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
只加了一个选项-d    {:7_158:}来列出目录,其它的选项我会在后续完善!

文件名   只列出当前目录
文件名 -d  目录名     列出指定目录

运行环境LINUX


  1. #include<unistd.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<dirent.h>
  6. #include<sys/stat.h>



  7. int main( int argc, char **argv )
  8. {
  9.        
  10.         void list_dir ( char *);

  11.         char         dir_name[20]={0};
  12.         int         list_dir_option = 0;
  13.         while ( *++argv != NULL && **argv == '-' ) {
  14.         /*
  15.         **dispose the parameter next '-'
  16.         **
  17.         */
  18.        
  19.                 switch (*++*argv) {
  20.                         case 'd':
  21.                                 list_dir_option = 1;
  22.                                 break;
  23.                         default :
  24.                                 printf("Parameter is error !\n");
  25.                                 exit (0);
  26.                 }
  27.         }
  28.         if ( *argv != NULL ) {
  29.                 strcpy ( dir_name, *argv );
  30.                 if ( list_dir_option )
  31.                         list_dir ( dir_name );
  32.                 else
  33.                         printf("Please input the parameter !\n");
  34.         }
  35.         else if ( *argv == NULL ) {
  36.                 if ( !list_dir_option )
  37.                         list_dir (".");
  38.                 else
  39.                         printf("Please input directory !\n");
  40.         }
  41.         else
  42.                 printf("Parameter is error !\n");
  43. }




  44. /*
  45. **To list the directory
  46. */
  47. void
  48. list_dir ( char *dir_names )
  49. {
  50.         DIR         *dp;
  51.         struct         dirent         *entry;
  52.         struct         stat        statbuf;

  53.         if ( ( dp = opendir( dir_names ) ) == NULL ) {
  54.                 fprintf ( stderr, "cannot open the directory:%s\n",dir_names );
  55.                 return;
  56.         }
  57.         chdir ( dir_names );        /*into directory*/
  58.         /*
  59.         **Loop ergdic this directory and list the dir or file
  60.         */
  61.         while ( ( entry = readdir ( dp ) ) != NULL ) {
  62.                 /*
  63.                 **To detection the dir
  64.                 */
  65.                 lstat (entry->d_name, &statbuf );
  66.                 if ( S_ISDIR (statbuf.st_mode) ) {
  67.                         /*
  68.                         **skip "." and ".."
  69.                         */
  70.                         if ( strcmp ( entry->d_name, "." ) == 0 ||
  71.                              strcmp ( entry->d_name, "..") == 0 )
  72.                                 continue;
  73.                         printf("%s/\t<----------------------[DIR]\n", entry->d_name );
  74.                 }
  75.                 else
  76.                         printf("%s\n", entry->d_name );
  77.                 }
  78.         chdir ("..");
  79.         closedir (dp);
  80. }
复制代码
list2.png

list1.png


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-9-16 13:46:53 | 显示全部楼层
个人感觉是不是调用系统命令像:ll,la之类的也可以啊。。。
要是带参数的话就用strcat连接起来,注意空格的话也可以吧
没有试过。。猜的。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2016-4-14 10:43:22 | 显示全部楼层
没学过
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 23:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表