1

私はCで始めて、関数realpathを使用する必要がありますが、多くの例が見つかりません。私はそれを自分で助けました:リンク。私の現在の問題は、私の関数はフォルダーに対して機能しますが、ファイルの場合は realpath が null を返すことです。

 while ((dir = readdir(rep)) != NULL)
    {
        if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") )
            {
                // do nothing (straight logic)
        } else {
            char buf[_POSIX_PATH_MAX];
            char *path;
            path = realpath(dir->d_name, buf);
            if (!path)
            {
                    perror("realpath");
                    exit(EXIT_FAILURE);
            }
    }

EDIT:私の機能の目的は、フォルダdir内のファイルとフォルダの絶対実パスを持つことです

4

1 に答える 1

1

問題は、に与えられた名前の一部として現在のディレクトリを渡すのに失敗していることですrealpath。単純に渡すことはできませんdir->d_name(現在のディレクトリの下にあるファイルまたはサブディレクトリの名前です)。への呼び出しで使用したディレクトリ名にdir->d_name(などを付けて) を追加する必要があります。例:strcatopendir

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>

#ifndef _POSIX_PATH_MAX
#define _POSIX_PATH_MAX  512
#endif

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

    if (argc < 2) {
        fprintf (stderr, "error: insufficient arguments.  usage: %s dirname\n",
                argv[0]);
        return 1;
    }

    char *p = argv[1];
    size_t len = strlen (p);
    while (*p && p[len-1] == '/')
        p[--len] = 0;

    DIR *rep = opendir (p);
    struct dirent *dir = NULL;

    if (!rep) {
        fprintf (stderr, "error: opendir failed on '%s'\n", p);
        return 0;
    }

    while ((dir = readdir(rep)))
    {
        if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") )
        {
            // do nothing (straight logic)
        }
        else {
            char buf[_POSIX_PATH_MAX] = {0};
            char entry[_POSIX_PATH_MAX] = {0};
            char *path = NULL;
            errno = 0;
            strcat (entry, p);
            strcat (entry, "/");
            strcat (entry, dir->d_name);
            printf ("getting realpath for : '%s'\n", entry);
            path = realpath (entry, buf);
            if (!path || errno)
            {
                perror("realpath");
                exit(EXIT_FAILURE);
            }
            else
                printf (" realpath for '%s' : %s\n", entry, buf);
        }
    }

    return 0;
}

特に注意してください:

    else {
        char buf[_POSIX_PATH_MAX] = {0};
        char entry[_POSIX_PATH_MAX] = {0};
        char *path = NULL;
        errno = 0;
        strcat (entry, p);
        strcat (entry, "/");
        strcat (entry, dir->d_name);
        printf ("getting realpath for : '%s'\n", entry);
        path = realpath (entry, buf);

は、現在のディレクトリentryを保持する文字列、区切り文字'/'、最後にdir->_dnameです。

例/出力

$ ./bin/realpathtst debug
getting realpath for : 'debug/ptrrtn.c'
 realpath for 'debug/ptrrtn.c' : /home/david/dev/src-c/tmp/debug/ptrrtn.c
getting realpath for : 'debug/structinit.c'
 realpath for 'debug/structinit.c' : /home/david/dev/src-c/tmp/debug/structinit.c
getting realpath for : 'debug/leetcode.c'
 realpath for 'debug/leetcode.c' : /home/david/dev/src-c/tmp/debug/leetcode.c
于 2015-11-05T21:53:55.040 に答える