【问题标题】:Parent directory of a file文件的父目录
【发布时间】:2011-07-20 13:09:16
【问题描述】:

有什么办法可以用C程序找出文件的父目录。我想为目录拥有的文件赋予相同的权限。为此,我想知道文件的父目录。任何帮助表示赞赏。

【问题讨论】:

  • 来自字符串?来自文件描述符?
  • 一个文件可以有多个父目录:参见 Linux 中的 man ln、Windows 中的连接点或其他任何内容(mklink /? 适用于我的 Windows Vista)
  • 如果你不给它任何权限,它会继承目录的权限。
  • 我不想知道整个层次结构..只有最底层
  • @pmg 是的,我们很幸运,我们现在在 Windows 上有了真正的符号链接!

标签: c


【解决方案1】:

如果你有文件的路径,你可以手动完成,如果它是相对路径(在 Unix 上不是以 / 开头,或者不是以 letter:\\letter:// 在 Windows 上),然后将其拆分为文件分隔符(/\),但我知道没有内置函数可以为您完成所有这些操作。

basename and dirname 函数可能会有所帮助,但您需要自己弄清楚文件的足够路径,因为它们仅适用于字符串;他们不询问文件系统。

【讨论】:

    【解决方案2】:

    不一定能做到正确的事情,但您是否尝试过以下任何一种方法:

    • 如果您的文件名包含路径分隔符(例如,Unix 上的 /,Windows 上的 \),使用例如复制字符串strdup() 并用零/空字符替换最后出现的路径分隔符(例如 strrchr())。结果字符串将是您文件的父目录。

    • 如果没有路径分隔符,则该文件位于您当前的工作目录中。您是否尝试过仅使用.... 链接适用于 Unix 和 Windows。

    上面有很多极端情况(例如,文件/hello.txt 是什么?),但它应该是一个开始。

    【讨论】:

      【解决方案3】:

      标准 C 中没有这样的功能。您可以在 Windows 上使用GetFullPathName 试试运气

      然后可能是_splitpath

      但正如所写的那样,做这类事情没有标准功能。

      【讨论】:

      • 实际上在 Windows 上,您可以在字符串末尾加入 ..,然后调用 PathCanonicalize()
      【解决方案4】:

      我使用cplusplus web pageGoz's answerthkala's answer 中的代码编写了一个C 语言sn-p。

      #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>
      #include <stdbool.h>
      #include <stdlib.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <unistd.h>
      #include <time.h>
      .
      .
      .
          out_file_name = "c:/Users/Documents/output/test.txt";
          const char ch = '/'; 
          char* rest;
          int loc; 
          char letter;
          char* pointer_to_loc; 
      
          // Current directory
          rest = strrchr(out_file_name, ch);
          // Just make sure there is a directory
          if (rest != NULL) {
              pointer_to_loc = strrchr(out_file_name, ch);  # Pointer to last / symbol
              loc = rest - out_file_name + 1;
              char subbuff[loc]; # string for truncated string
              loc--;
              memcpy( subbuff, &out_file_name[0], loc ); # copy section of string
              subbuff[loc] = '\0';
      
              // Parent directory, now input is current directory from previous section
              rest = strrchr(subbuff, ch);
              if (rest != NULL) {
                  loc = rest - subbuff + 1;
                  char subsubbuff[loc];
                  loc--;
                  memcpy( subsubbuff, &subbuff[0], loc );
                  subsubbuff[loc] = '\0';
      
                  printf (subsubbuff); // Parent directory
                  printf ("\n");
              }
              printf (subbuff); // Current directory
              printf ("\n");
          }
          printf (output_file_name); // Entire path to file
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2013-05-22
        • 2013-05-25
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        相关资源
        最近更新 更多