Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everyone: I need to list all the files and folders that exists in a folder. Just like the LS function, however I need to do this is c++, without ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    6

    getting files and folders list

    Hi everyone:

    I need to list all the files and folders that exists in a folder. Just like the LS function, however I need to do this is c++, without doing system calls.

    What I actually need is to find a way to get into an array of strings, the name of each file and folder inside a certain folder.

    Any idea how to do this?

    Thank you

    HW

  2. #2
    Just Joined!
    Join Date
    Jan 2007
    Posts
    23

    listing files

    hi,

    this may be bit useful...

    int main(int argc, char **argv)
    {

    if(argc<2){
    printf("Usage:%s<pathname>\n",argv[0]);
    exit(1);
    }

    fsize(*++argv);
    printf("The pathname of the directory is argv=%s\n", *argv);

    return 0;
    }


    void fsize(char *name)
    {
    struct stat stbuf;

    if(stat(name,&stbuf)== -1){
    fprintf(stderr,"fsize:can't access %s\n",name);
    return;
    }

    if((stbuf.st_mode& S_IFMT)==S_IFDIR)
    dirwalk(name,fsize);
    else
    printf("Not a directory !!\n");

    printf(" Given directory is %s and its size %8ld \n",name,stbuf.st_size);

    }

    void dirwalk(char *dir, void(*fcn)(char *))
    {

    struct dirent **namelist;
    struct stat stbuf;
    count=scandir(dir,&namelist,NULL,alphasort);
    if(count<=0)
    printf("There are no file in this directory\n");
    else
    printf("There are %d files in the directory\n",count);


    --------------------------------------------------------------------------

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    6
    thank you, I'll try it now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...