linux c++ 遍历目录下所有文件

void showAllFiles( const char * dir_name)
{  
    if( NULL == dir_name )
    {  
        cout<<" dir_name is null ! "<<endl;
        return;
    }

    struct stat s;  
    lstat( dir_name , &s );  
    if( ! S_ISDIR( s.st_mode ) )  
    {
        return;
    }
      
    struct dirent * filename;
    DIR * dir;
    dir = opendir( dir_name );  
    if( NULL == dir )  
    {  
        return;  
    }  

    int iName=0;
    while( ( filename = readdir(dir) ) != NULL )  
    {  
        if( strcmp( filename->d_name , "." ) == 0 ||
            strcmp( filename->d_name , "..") == 0)
            continue;

        char wholePath[128] = {0};
        sprintf(wholePath, "%s/%s", dir_name, filename->d_name);

        cout << "wholePath= " << wholePath << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/huangjiazhi_/article/details/80501233