With files in the output directory traversal linuxC language folders and files

  • Experimental environment linux mint development platform Qt5.11
  • The general idea, linux C file directory related functions mkdir rmdir opendir readdir
  • File directory pointer type DIR *
  • dirent represents the system file directory structure associated, wherein the attribute file type d_type d_name DT_DIR file name or directory name represents a file directory, DT_REG representative of a normal file
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <dirent.h> 
#include <SYS / types.h> int GETDIR ( char * pathname) 
{ 
    the DIR * path = NULL ; 
    path = the opendir (pathname); IF (path == NULL) 
    { 
        perror ( " failed " ); 
        Exit ( . 1 ); 
    } struct dirent * PTR; // directory structure --- properties: directory type d_type, directory name d_name char buf [ 1024



    
    
    ]={0};
    while((ptr=readdir(path))!=NULL)
    {
        if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)
        {
            continue;
        }
        //如果是目录
        if(ptr->d_type==DT_DIR)
        {

            sprintf(buf,"%s/%s",pathname,ptr->d_name);
            printf("目录:%s\n",buf);
            getdir(buf);
        }
        if (Ptr-> == d_type DT_REG) 
        { 
            sprintf (buf, " % S /% S " , pathname, ptr-> d_name); // the pathname and filename character array buffer spliced into 
            the printf ( " file: S% \ n- " , buf); 
        } 
    } 
    return  0 ; 
} 
int main () 
{ 
    GETDIR ( " / Home / CPC / Pictures " );
     return  0 ; 
}

 

Output:

Guess you like

Origin www.cnblogs.com/saintdingspage/p/12159336.html