开发者

Parent directory of a file

Is there any way to find out the parent directory of a file using C program. I would like to give same permissions for the file that the directory has. So as to do so, i wou开发者_StackOverflow中文版ld like to know the parent directory of the file. Any help is appreciated.


If you have the path of the file, you can do it manually by making it an absolute path if it is relative (doesn't begin with a / on Unix, or a letter:\ or \ or letter:/ or / on Windows) and then splitting it on file separator characters (/ or \), but I am aware of no built-in function that will do all of this for you.

The basename and dirname functions may help, but you'll need to figure out enough of the path of the file yourself, as they only work with strings; they do not interrogate the file system.


It's not guaranteed to do The Right Thing, but have you tried any of the following:

  • If your filename contains a path separator (e.g. / on Unix, \ on Windows), copy the string using e.g. strdup() and replace the last occurence of the path separator (found with e.g. strrchr()) with a zero/null character. The resulting string will be the parent directory of your file.

  • If there is no path separator, then the file resides within your current working directory. Have you tried just using .? The . and .. links work on both Unix and Windows.

There are quite a few corner cases above (e.g. what of the file /hello.txt?), but it should be a start.


There is no such function in Standard C. You may try your luck on Windows with GetFullPathName

and then maybe _splitpath

But as written there's not standard function for doing such kind of things.


I wrote a C language snippet using codes from cplusplus web page, Goz's answer and thkala's answer.

#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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜