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 would like to know the parent directory of the file. Any help is appreciated.
-
1From string? From file descriptor? – artyom.stv Mar 22 '11 at 14:25
-
2A file can have more than 1 parent directory: see `man ln` in Linux, junction point or whatever in Windows (`mklink /?` works on my Windows Vista) – pmg Mar 22 '11 at 14:29
-
If you don't give it any permissions, it will inherit those of the directory. – David Heffernan Mar 22 '11 at 14:31
-
i dont want to know the entire hierarchy..only the bottom most – nikhil Mar 22 '11 at 14:31
-
@pmg yeah lucky us, we've got real symlinks now on Windows!! – David Heffernan Mar 22 '11 at 14:39
-
@David Not to start an argument, but you can't `cd` into a junction directory, so I wouldn't say they're real :-P – Jonathan Mar 22 '11 at 14:41
-
Which operating system and file system are you using? Standard C does not provide what you want. – Peter G. Mar 22 '11 at 14:42
-
@Jonathan That's junctions. You are behind the times. We do have symlinks now in Windows 7. And you can cd into a junction directory as make by `junction`. It just did so. – David Heffernan Mar 22 '11 at 14:43
-
@David Neat! Last I tried, it told me it couldn't, but that was about a year ago in Vista. – Jonathan Mar 22 '11 at 14:45
-
@Jonathan I've never seen that, way back to win2k, but who knows with junctions. They are a bit weird. – David Heffernan Mar 22 '11 at 14:47
4 Answers
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.

- 13,354
- 4
- 36
- 32
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.

- 84,049
- 23
- 157
- 201
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.
-
Actually on Windows you would join a .. to the end of the string and then call `PathCanonicalize()`. – David Heffernan Mar 22 '11 at 14:48
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

- 1,594
- 19
- 22