Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   files in directory (http://www.go4expert.com/forums/files-in-directory-t5512/)

rajkumarmadhani 28Jul2007 16:48

files in directory
 
hi,

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

DaWei 28Jul2007 18:25

Re: files in directory
 
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).

rajkumarmadhani 29Jul2007 19:19

Re: files in directory
 
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.

Quote:

Originally Posted by DaWei
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).



All times are GMT +5.5. The time now is 10:20.