C 语言编写的Windows下 类似Linux 的 ls(list) 命令


C 语言编写的Windows下 类似Linux 的 ls(list) 命令.

命令格式:

ls directory_name  (directory_name 就是文件夹目录)

如使用有问题, 欢迎提问.

代码如下

  1. int main(int argc, char *argv[])  
  2. {  
  3.     DIR *dp;  
  4.     struct dirent *dirp;  
  5.       
  6.       
  7.     // check the enter   
  8.     if (argc != 2)  
  9.     {  
  10.         printf("Error: enter false!!!!\nRetry:");  
  11.         return 1;  
  12.     }  
  13.       
  14.     // check the director   
  15.     if ( (dp = opendir(argv[1]) ) == NULL)  
  16.     {  
  17.         printf("The is not %s directory", argv[1]);  
  18.         return 1;  
  19.     }  
  20.       
  21.     // list the file name   
  22.     while ( (dirp = readdir(dp)) != NULL)  
  23.     {  
  24.         printf("%s\n", dirp -> d_name);  
  25.     }  
  26.       
  27.     closedir(dp);  
  28.     return 0;  
  29. }  

相关内容