程序让生活更美好

半亩方塘 天光云影

  C++博客 ::  :: 新随笔 :: 联系 ::  :: 管理 ::
  55 随笔 :: 4 文章 :: 202 评论 :: 0 Trackbacks
#include   <stdio.h> 
#include   
<dirent.h> 
#include   
<string.h> 

 typedef   
struct   FileList 
    

        
char   filename[64]; 
        
struct   FileList   *next; 
    }
FILENODE; 

FILENODE
*  getFiles(char *dir/*文目录*/)
{
   DIR   
*directory_pointer; 
   
struct   dirent   *entry; 
   directory_pointer
=opendir(dir);
   
struct FileList start;
   
struct FileList *filesNode;
    start.next
=NULL; 
        filesNode
=&start; 
        
while   ((entry=readdir(directory_pointer))!=NULL) 
        

            filesNode
-> next=(struct   FileList   *)malloc(sizeof(struct   FileList)); 
            filesNode
=filesNode-> next; 
            strcpy(filesNode
-> filename,entry-> d_name); 
            filesNode
-> next=NULL; 
        }
 
        closedir(directory_pointer); 
        filesNode
=start.next;
        
return filesNode;         
}


int   main() 

    
struct FileList *filesNode;
    
char dir[100]="D:\\down";  
    filesNode
=getFiles(dir);
   
    
    
if (filesNode==NULL)
    
{
    printf(
"没有成功");
    
return 0;
    }

    
while(filesNode) 
    

    printf( 
"%s\n ",filesNode-> filename); 
    filesNode
=filesNode-> next; 
    }
  
  
    system(
"pause");
    
return 0;
}

posted on 2008-08-28 18:10 北风之神007 阅读(5287) 评论(2)  编辑 收藏 引用 所属分类: c/c++

评论

# re: C语言获取目录中文件名 2012-10-07 22:42 得到
可以用,好文  回复  更多评论
  

# re: C语言获取目录中文件名 2014-03-20 11:31 13*0217
来个精简版的:

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
DIR * dir;
struct dirent * ptr;
int i;
dir = opendir("d:\\");
while((ptr = readdir(dir)) != NULL)
{
printf("%s\n", ptr->d_name);
}
closedir(dir);
return 0;
}  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理