This question may be a bit naive, but I'm wondering if it's possible to obtain the directory part of a filename in C code. For instance, say I have the following Linux file:
/home/user/Documents/Work/report.txt
and I want to obtain the string "/home/user/Documents/Work/"
Are there any C functions to do such a thing? Also, even though I gave a Linux file as an example, I'd like to be able to obtain the directory listing of a file on any OS.
Thank you in advance.
strrchr()
.strrchr()
. But really, use the right tool for the task -dirname()
. See my answer.