C 文件的父目录

C 文件的父目录,c,C,有没有办法用C程序找出文件的父目录。我想为该目录拥有的文件授予相同的权限。为此,我想知道文件的父目录。非常感谢您的帮助。标准C中没有此类功能。您可以在Windows上使用以下功能试试运气: GetFullPathName 然后可能_splitpath 但是,正如所写的那样,没有标准的函数来做这类事情。如果你有文件的路径,你可以手动做它,如果它是相对的(在Unix上不是以/开头,在Windows上不是以字母:\或\或字母:/或/开头)然后在文件分隔符(/或\)上拆分它,但我知道没有内置函数可以为

有没有办法用C程序找出文件的父目录。我想为该目录拥有的文件授予相同的权限。为此,我想知道文件的父目录。非常感谢您的帮助。

标准C中没有此类功能。您可以在Windows上使用以下功能试试运气: GetFullPathName

然后可能_splitpath


但是,正如所写的那样,没有标准的函数来做这类事情。

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

这些函数可能会有所帮助,但您需要自己找出足够的文件路径,因为它们只处理字符串;他们不会询问文件系统。

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

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

  • 如果没有路径分隔符,则文件位于当前工作目录中。您是否尝试过使用
    链接在Unix和Windows上都可以工作


上面有很多例子(例如文件
/hello.txt
?),但这应该是一个开始。

我用和中的代码编写了一个C语言片段

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
.
.
.
out\u file\u name=“c:/Users/Documents/output/test.txt”;
const char ch='/';
char*rest;
int loc;
字符字母;
字符*指向位置的指针;
//当前目录
rest=strrchr(输出文件名,ch);
//只要确保有一个目录
if(rest!=NULL){
指向_loc=strrchr(out文件名,ch)#指向last/symbol的指针
loc=剩余文件名+1;
char subbuff[loc];#用于截断字符串的字符串
loc——;
memcpy(subbuff,&out_file_name[0],loc);#复制字符串的部分
子buff[loc]='\0';
//父目录,现在输入上一节中的当前目录
rest=strrchr(次抛光,ch);
if(rest!=NULL){
loc=静止-次缓冲+1;
半焦亚焦[loc];
loc——;
memcpy(子缓冲区和子缓冲区[0],loc);
subsubbuff[loc]='\0';
printf(subsubbuff);//父目录
printf(“\n”);
}
printf(subbuff);//当前目录
printf(“\n”);
}
printf(输出文件名);//文件的完整路径

来自字符串?从文件描述符?一个文件可以有多个父目录:请参阅Linux中的
man ln
,Windows中的连接点或任何东西(
mklink/?
适用于我的Windows Vista)。如果您不授予它任何权限,它将继承目录的权限。我不想知道整个层次结构。只想知道底部most@pmg是的,我们很幸运,我们现在在Windows上有了真正的符号链接!!实际上,在Windows上,您将加入。。到字符串末尾,然后调用
pathcononicalize()
#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