Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c - 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 would like to know the parent directory of the file. Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...