天下

记录修行的印记

Linux目录遍历

#include "unistd.h"
#include 
<iostream>
#include 
<sys/types.h>
#include 
<sys/stat.h>
#include 
<dirent.h>

using namespace std;

#define USER_LIST_FILE "./UserInfo.txt"
#define USER_LIST_PHOTODIR "./user-photo"
void GetUserList()
{
    FILE
* fp = NULL;
    DIR
*  pDir = NULL;
    fp 
= fopen(USER_LIST_FILE,"r");
    
if (fp==NULL)
    {
        printf(
"file open fail \r\n");
        
return;
    }

    pDir 
= opendir(USER_LIST_PHOTODIR);
    
if (pDir==NULL)
    {
        printf(
"dir open fail \r\n");
        
return;
    }
     
    
struct dirent *pDirent;
    
while((pDirent = readdir(pDir)) != NULL)
    {
        
if (pDirent->d_type == DT_REG)//普通文件
        {
            
        }
        printf(
"d_type:%d,d_name: %s\n",pDirent->d_type,pDirent->d_name);
    }

}
int main()
{
    GetUserList();
    
return 0;
}

结构体
/* File types for `d_type'.  */
enum
{
    DT_UNKNOWN 
= 0,
    DT_FIFO 
= 1,
    DT_CHR 
= 2,
    DT_DIR 
= 4,
    DT_BLK 
= 6,
    DT_REG 
= 8,
    DT_LNK 
= 10,
    DT_SOCK 
= 12,
    DT_WHT 
= 14
};



posted on 2012-03-25 10:57 天下 阅读(921) 评论(2)  编辑 收藏 引用 所属分类: Linux编程

评论

# re: Linux目录遍历 2012-06-27 17:07 pdkui

少了closedir(pDir)  回复  更多评论   

# re: Linux目录遍历 2012-06-28 14:40 13174115

@pdkui
呵呵,多谢提醒!
  回复  更多评论   


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


<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论