• Linux:遍历文件目录


    
    
    /**
     * 扫描一个目录
     */
     
    #include <dirent.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    
    static void list_files(const char *dir_path)
    {
    	DIR *ptr_dir = NULL;
    	struct dirent *ptr_dirent = NULL;
    	
    	ptr_dir = opendir(dir_path);
    	if (NULL == dir_path)
    	{
    		printf("opendir %s failed.
    ",  dir_path);
    		return;
    	}
    	
    	while (1)
    	{
    		errno = 0;
    		ptr_dirent = readdir(ptr_dir);
    		if (NULL == ptr_dirent)
    		{
    			break;
    		}
    		
    		/* 跳过"."和".."目录 */
    		if (strcmp(ptr_dirent->d_name, ".") == 0 || strcmp(ptr_dirent->d_name, "..") == 0)
    		{
    			continue;
    		}
    		
    		printf("%s/%s
    ", dir_path, ptr_dirent->d_name);
    	}
    	
    	if (errno != 0)
    	{
    		printf("readdir error, errno=%d.
    ", errno);
    	}
    	
    	closedir(ptr_dir);
    	return;
    }
    
    int main(int argc, char *argv[])
    {
    	/* 输入参数检查&提示 */
    	if ((argc > 1) && (0 == strcmp(argv[1], "--help")))
    	{
    		printf("%s [dir...]
    ", argv[0]);
    		return -1;
    	}
    
    	if (argc == 1)
    	{
    		list_files(".");
    	}
    	else
    	{
    		for (argv++; *argv != NULL; argv++)
    		{
    			list_files(*argv);
    		}
    	}
    
    	return 0;
    }
    

    参考资料

    • Linux/UNIX系统编程手册(上册)
  • 相关阅读:
    MyCat清单
    Nginx整合Tomcat
    Nginx安装与配置
    Spring清单
    Shiro清单
    Dubbo清单
    MyBatis清单
    查询数据库的编码
    myBatis
    面试
  • 原文地址:https://www.cnblogs.com/hustluotao/p/15426375.html
Copyright © 2020-2023  润新知