I want to call opendir()
with the argument of the directory the program is being run from. If possible it will be a platform independent way. I'm confused because the accepted answer to a similar question doesn't look to be platform independent. Other answers say use Boost but a reply to this question states initial_path()
is deprecated. If it can't be done in a platform independent the target is Windows.
Asked
Active
Viewed 181 times
0
-
Have you tried `opendir(.)` with the literal dot '.' for the current working directory? – MartyE Aug 15 '12 at 21:32
-
You can't have a *totally* platform independent path handling, simply beause not all platforms have paths. :-) – Bo Persson Aug 15 '12 at 21:38
-
The second question you link to has the answer: Get the current directory when your program starts, and remember it for later when you need it. A comment confirms that Boost's `current_path` can help you with the first part of that task. The first question's accepted answer is simply another implementation of `current_path`; the answer just neglects to point out that you should call it early in your program and then refer to the saved result. – Rob Kennedy Aug 15 '12 at 21:40
3 Answers
1
This is cross platform, and tells you the program's current working directory.
char *getcwd(char *buf, size_t size);
This is a posix function, and should be supported on Linux, OsX and Windows. Read about it here:

Rafael Baptista
- 11,181
- 5
- 39
- 59
-
-
size_t is generally the size of a register on most platforms. size_t will work on linux and OsX. Use DWORD on Windows. – Rafael Baptista Aug 15 '12 at 21:39
-
@Celeritas: You pass the size of `buf` as the second parameter so that it does over-run the end of the buffer. – Martin York Aug 15 '12 at 21:54
1
initial_path()
is deprecated, but it effectively called current_path()
at the beginning of the program for you and then returned that result on any subsequent calls. So all you need to do is call current_path()
at the beginning of your program and store it somewhere.

Jesse Good
- 50,901
- 14
- 124
- 166
0
Approximately the same question is here.
You can also get help from here: Makefile Tutorial
You can use these commands as well
initial_path()
current_path()