0

I am searching directories in current directory and if I accross a directory , I got in and search directories and files again but I cannot find the problem.I always get the same result and some specific directories. In code I get directories name in array and print them.The result looks like this ;

direc Num (except parent and curr) : 6

/home/sabri/Desktop/Test/Untitled Folder 3

/home/sabri/Desktop/Test/Untitled Folder 3

/home/sabri/Desktop/Test/Untitled Folder 3

/home/sabri/Desktop/Test/Untitled Folder 3

/home/sabri/Desktop/Test

/home/sabri/Desktop/Test/Untitled Folder 4

/home/sabri/Desktop/Test/Untitled Folder 4

/home/sabri/Desktop/Test/Untitled Folder 4

but my directories are ;

Untitled Folder 1

Untitled Folder 2

Untitled Folder 3

Untitled Folder 4

Untitled Folder 5

int listFilesIndir(char *currDir) 
{
    struct dirent *direntp;
    char newDir[20][250];


    DIR *dirp;
    int x ,y =0,i=0 ;


    if ((dirp = opendir(currDir)) == NULL) 
    {
        perror ("Failed to open directory");
        return 1;
    }

    while ((direntp = readdir(dirp)) != NULL)
    {
        printf("%s\n", direntp->d_name);
        if(direntp->d_type == DT_DIR)
        {
            y++;
            chdir(direntp->d_name);
            getcwd(newDir[i],250);
            listNewDir(direntp->d_name);
            i++;

        }
    }

    printf("direc Num (except parent and current) : %d\n",y-2 );

    for ( i = 0; i < 10; ++i)
    {
        printf("%s\n", newDir[i]);
    }

    while ((closedir(dirp) == -1) && (errno == EINTR)) ;

    return 0;
}


int listNewDir(char *currDir) 
{
    struct dirent *direntp;
    DIR *dirp;

    if ((dirp = opendir(currDir)) == NULL) 
    {
        perror ("Failed to open directory");
        return 1;
    }

    while ((direntp = readdir(dirp)) != NULL)
        printf("%s\n", direntp->d_name);

    while ((closedir(dirp) == -1) && (errno == EINTR)) ;

    return 0;
}
Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
  • You may find that [ftw][1] (file tree walk) helps here! [1]: http://linux.die.net/man/3/ftw – Joe Apr 02 '13 at 08:48
  • @joe: When adding a comment, click the "help" link to see how to embed links in a comment - it is not the same as the Markdown syntax used in Q&A editor. – Clifford Apr 02 '13 at 09:40
  • @Clifford Ta, I wondered what I'd screwed up! :-) – Joe Apr 02 '13 at 09:42
  • I am not sure the tags on this question are relevant or helpful. Even if this is for an embedded system for example, the question is general in nature and should not perhaps be targeted at the embedded space - that will just narrow your audience unnecessarily. I suggest just "C" and "Linux" or perhaps "POSIX" since that appears to be the API in use. – Clifford Apr 02 '13 at 09:45

1 Answers1

0

You need to skip the '.' and '..' entries. You also shouldn't do the chdir and getcwd. Just passing the directory name to your listNewDir function will print the files in that directory. (You can just strcpy the name into newDir if you want to save the name.)
Also, the for loop only goes up to 10 and the newDir is only good for 20 entries. I presume that those are only for testing.

No One in Particular
  • 2,846
  • 4
  • 27
  • 32