【问题标题】:My realpath return null for files我的真实路径为文件返回 null
【发布时间】:2015-11-05 17:15:08
【问题描述】:

我从 C 开始,我需要使用函数 realpath,但我没有找到很多示例。我帮了自己:link。 我目前的问题是我的函数适用于文件夹,但当它是文件时,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);
            }
    }

编辑:我的功能的目的是为文件夹目录中的文件和文件夹提供任何绝对真实路径

【问题讨论】:

  • 您需要在调用realpath 后检查errno 以获取EACCES, EINVAL, EIO, ELOOP, ENAMETOOLONG, ENOENTENOTDIR 以找出原因。我怀疑你会发现它是ENOTDIR
  • 当我这样做时:printf("ERROR: %s\n", strerror(errno)); 我有:No such file or directory。我确定该文件存在。
  • 你的工作目录是什么? dir->d_name 只是基本名称 - 最后一个“/”之后的文件名。
  • 不,文件名是 dir->d_name。我的工作目录是 rep 或 dir after。

标签: c unix realpath


【解决方案1】:

问题是您未能将当前目录作为给realpath 的名称的一部分传递。您不能简单地传递dir->d_name(这只是当前目录下的文件或子目录的名称)。您必须将dir->d_name(与strcat 等)附加到您在调用opendir 时使用的目录名称。示例:

#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-&gt;_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

【讨论】:

  • 谢谢,我正在努力,因为目前我的代码已经转储了核心。
  • 我不知道您是如何指定要查找的文件的上层目录的?我需要知道它在哪里,才能找到确切的目录吗?如果我想搜索我不知道任何上层目录的所有可能的 unix 终端命令怎么办?
  • 我将要打开的目录作为给程序的第一个参数,例如char *p = argv[1];(我将指针p设置为指向命令行上给定的目录)然后当我strcat (entry, p);我先把目录放在entry中,然后用strcat (entry, "/");和@添加分隔符和文件名987654337@如果您仍然卡住,请告诉我。
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多