0

特定のファイルを検索するために、Ubuntuのターミナルからコードを実行していますが、検索により、そのフォルダー内だけでなく外部にもフォルダーとファイルが生成されます。コードは次のとおりです。

(コンパイルして実行可能)

#include <stdio.h>     // For perror
#include <stdlib.h>
#include <sys/types.h> // For stat, opendir, readdir
#include <sys/stat.h>  // For stat
#include <unistd.h>    // For stat
#include <dirent.h>    // For opendir, readdir


int displayAllFiles(char * directory)
{

    DIR *dir;
    struct dirent *ent;
        dir = opendir (directory);
    if (dir != NULL) {

      /* print all the files and directories within directory */
      while ((ent = readdir (dir)) != NULL) {
        printf ("%s\n", ent->d_name);
      }
      closedir (dir);
    } else {
      /* could not open directory */
      perror ("");
      return EXIT_FAILURE;
    }

    return 1;

}



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

    DIR *dir;
    struct dirent *entry;
    int result;
    struct stat status;
    char path[PATH_MAX];

    dir = opendir(argv[1]);
    if (!dir)
    {
        perror("opendir");
        exit(1);
    }

    entry = readdir(dir);
    while (entry)
    {
        result = snprintf(path, sizeof(path), "%s", argv[1]);
        snprintf(&path[result], sizeof(path) - result, "/%s", entry->d_name);

        int out = displayAllFiles(path);
        printf("%s", path);

        result = lstat(path, &status);
        if (-1 == result)
        {
            printf("\n");
            perror("stat");
            exit(2);
        }

        if (S_ISLNK(status.st_mode))
        {
            printf("%s", " is a symbolic link");
        }

        printf("\n");

        entry = readdir(dir);
    }


    return(0);
}

ターミナルからヒットした./exer4 checkそのフォルダーcheckは、親フォルダーであり、次に:

  • insideCheck内側にありますcheck
  • moreInside内側にありますinsideCheck
  • そしてdavid.txt中にいるmoreInside

しかし、私が得るものは次のとおりです。

a@ubuntu:~/Desktop$ ./exer4 check
check
..
Link to workspace2
eclipse 4 linux
fol
.
basherFolder
Link to eclipse
test
eclipse
exer4
doc.txt~
check/..
..
.
insideCheck
check/.
..
moreInside
.
david.txt~
check/insideCheck

何かがおかしいのはわかりますが、何が原因かわかりません。

よろしくお願いします。

4

1 に答える 1

4

これは、親ディレクトリを意味する現在のディレクトリにもリストされているため.に発生します。....

あなたのコメントへの答え: パスを.orと比較すると..うまくいきません。Directory/.Directory/..

while (entry)
{
    result = snprintf(path, sizeof(path), "%s", argv[1]);
    snprintf(&path[result], sizeof(path) - result, "/%s", entry->d_name);

    char cur_str[80];
    char par_str[80];
    strcpy ( cur_str , argv[1] );
    strcat ( cur_str , "/." );

    strcpy ( par_str , argv[1] );
    strcat ( par_str , "/.." );

    if (strcmp(path,cur_str) == 0 || strcmp(path ,par_str) == 0) {
     break;
    }
    int out = displayAllFiles(path);
    printf("%s", path);

    result = lstat(path, &status);
    if (-1 == result)
    {
        printf("\n");
        perror("stat");
        exit(2);
    }
    if (S_ISLNK(status.st_mode))
    {
        printf("%s", " is a symbolic link");
    }

    printf("\n");

    entry = readdir(dir);
}

上記のコードは機能します。(ただし、ディレクトリではないファイルには「ディレクトリではありません」というエラーが表示されます)。

于 2012-06-15T19:08:51.473 に答える