共有回帖数  0  个 
	 
	
	
	
     
          
          
               
				
			 
				
					 
 
            
				   - 
						
						
							 
									也许很多人知道有一种叫“设计模式”的东西,也有许多人知道它很重要。可是网上找教程看,买书背,到了实际的编程又感觉怎么也结合不到自己的程序中。
其实,设计模式呢,它是有2层意思,第一层是“设计”,也就是说程序得先设计,然后再应用各种模式。
今天从一个简单的问题看下怎么更好得设计程序,应用设计模式吧!
现在假设我们写一个程序,其实有一个功能是需要遍历一个目录下的所有目录的所有文件,把里面的文件名给打印出来。
小伙伴们,有人说说怎么写吗??
有小伙伴说,这还不简单吗,分分钟写出一个遍历一个目录下的功能出来,比如:
#include stdio.h 
#include dirent.h 
#include sys/types.h 
#include sys/stat.h 
#include unistd.h
#include stdlib.h
#include string.h
#include assert.h
/**
* 码客原创
*/
int count = 0;
void listAllFiles(char *dirname);
int main(int argc, char *argv[]) { 
listAllFiles("./"); 
return 0;
} 
void listAllFiles(char *dirname) {
assert(dirname != NULL);
char path[512];
struct dirent *filename;
DIR *dir;
dir = opendir(dirname);
if(dir == NULL){
printf("open dir %s error!n",dirname);
return;
}
while((filename = readdir(dir)) != NULL) {
if(!strcmp(filename-d_name,".")||!strcmp(filename-d_name,"..")) {
continue;
}
sprintf(path,"%s/%s",dirname,filename-d_name);
struct stat s;
lstat(path,&s);
if(S_ISDIR(s.st_mode)) {
listAllFiles(path);
}
else {
printf("%d. %sn",++count,path);
}
}
closedir(dir);
}
							 
							 
							 
							  
							  
							  楼主 2016-03-31 08:31 回复
						 
						 
           
          
          
         
   
         
      
 
   
             
                  
                  
 
 
 
     
	 
  
	Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
	
	意见反馈 | 
	关于直线 | 
	版权声明 | 
	会员须知