博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nullnullhow to read directory name using std c in the linux
阅读量:6904 次
发布时间:2019-06-27

本文共 3209 字,大约阅读时间需要 10 分钟。

在改章节中,我们主要介绍nullnull的内容,自我感觉有个不错的建议和大家分享下

    每日一道理
生活的无奈,有时并不源于自我,别人无心的筑就,那是一种阴差阳错。生活本就是矛盾的,白天与黑夜间的距离,春夏秋冬之间的轮回,于是有了挑剔的喜爱,让无奈加上了喜悦的等待。
#include 
#include
#include
#include
#include
#define err_quit printf#define err_sys printf#define TRUE 1#define FALSE 0#define EXIT_FAILE 0#define EXIT_SUCESS 1#define bool charstatic char g_cur_dir[256] = "";int read_directory(char *dir_name, bool brecurse);void usage(char state, char *str);int main(int argc, char **argv){ int c; char recurse = 0; struct option long_option[]= { {"dirname", no_argument, NULL, 0 }, {"recurse", optional_argument, NULL, 'r'}, {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0 }, }; //read current directory. //if no any params, read current dir files. if(argc < 2) { read_directory("./", TRUE); return ; } //read command line params. while((c = getopt_long(argc, argv, "hr::", long_option, NULL)) != EOF) //while((c = getopt(argc, argv, ":hr::")) != EOF) { switch(c) { case 'r': recurse = 1; break; case 'h': usage(EXIT_SUCESS, argv[0]); break; default: usage(EXIT_FAILE, argv[0]); break; } } if (recurse && (optind == argc)) { read_directory("./", TRUE); exit(0); } //read all files from argv[optind]. for(; optind < argc; ++optind) { read_directory(argv[optind], recurse); } exit(0);}int read_directory(char *dir_name, bool brecurse){ DIR *dp; struct dirent *dir; strcpy(g_cur_dir, dir_name); if (g_cur_dir[strlen(g_cur_dir) -1] == '/') g_cur_dir[strlen(g_cur_dir) -1] = 0; if ((dp = opendir(dir_name)) == NULL) { err_sys("can't open %s. \n", dir_name); return ; } while( (dir = readdir(dp)) != NULL) { if ( brecurse && (DT_DIR == dir->d_type) && (strcmp(dir->d_name, ".") != 0) && (strcmp(dir->d_name, "..") != 0) ) { printf("%s/", g_cur_dir); printf("%s\n", dir->d_name); strcat(g_cur_dir, "/"); strcat(g_cur_dir, dir->d_name); read_directory(g_cur_dir, brecurse); g_cur_dir[strlen(g_cur_dir) - strlen(dir->d_name) - 1] = 0; } else { printf("%s/", g_cur_dir); printf("%s\n", dir->d_name); } } closedir(dp);}void usage(char state, char *str){ if (state == EXIT_FAILE) { printf("\n"); } else { printf("\n"); printf("========================================\n"); printf("read directory:\n"); printf("\n"); printf("Usage: %s [-r] [directory]\n\n", str); printf(" -r: read all files under each directory, recursively. \n"); printf("========================================\n"); printf("/n"); exit(0); }}

    

 

文章结束给大家分享下程序员的一些笑话语录: 看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。

转载地址:http://nqrdl.baihongyu.com/

你可能感兴趣的文章
事务详解
查看>>
Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入
查看>>
Excel 统计IP
查看>>
Lucene全文检索之-Lucene基础
查看>>
Java基于Socket文件传输示例
查看>>
spring01
查看>>
三,移植uboot-支持NAND启动
查看>>
vs2017报错LNK2005和LNK1169: 符号已多次定义
查看>>
ssh文件传输命令:sz与rz命令
查看>>
代理与反向代理
查看>>
CentOS7 使用ifconfig命令 ENS33没有IP地址的解决办法
查看>>
PHP防止SQL注入的方法
查看>>
jmeter接口测试
查看>>
文件夹设置“以前的版本”功能(配置卷影副本)
查看>>
PHP:第二章——PHP中的while语句
查看>>
043——VUE中组件之使用.sync修饰符与computed计算属性实现购物车原理
查看>>
基于Lucene的查询索引
查看>>
华为的四幅广告
查看>>
swift 学习- 27 -- 访问控制
查看>>
python 异常
查看>>