files in directory

Discussion in 'C' started by rajkumarmadhani, Jul 28, 2007.

  1. rajkumarmadhani

    rajkumarmadhani New Member

    Joined:
    Jul 28, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    how to access the files in a dirctory using C
    if anybody know please give me the answer.
    bye
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Now that's going to depend on you system and operating system, isn't it? Information is key. Perhaps you should read the "Before You Make A Query" thread (the title seems descriptive, but maybe that's just me).
     
  3. rajkumarmadhani

    rajkumarmadhani New Member

    Joined:
    Jul 28, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thank you for reply.

    iam using Linux OS.and i got answer.
    Code:
    printdir(char *dir,int depth)
    {
             DIR *dp;
             struct dirent *entry;
             struct stat sra;         
    
            if(dp=opendir(dir)==NULL)
            {
                           perror("error");
                           return;
            }
            chdir(dir);
            while((entry=readdir(dp))!=NULL)
            {
                  lstat(entry->d_name,&sta);
                  if(S_ISDIR(sta.st_mode))
                  {
                   if((strcmp(".",entry->s_name)==0) || (strcmp("..",entry->s_name)==0) )                   continue;
    
                           printf("%*s%s/",depth,"",entry->d_name);\
                           printdir(entry->d_name,depth+4);
                  }
                  else
                  {
                           printf("%*s%s",depth,"",entry->d_name);
                  }
           
    }
    main()
    {
           printfdir("/home",0);
           return 0;
    }
    thanx for reply sir.

     
    Last edited by a moderator: Jul 30, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice