Matt Sheppard
Matt Sheppard

Reputation: 118053

How can a currently running C program find out what directory it is located in?

Say I have a command line C program which is currently executing, and I want to read a file or execute another binary in the same directory - how can I find out what directory that is?

Note that I'm not looking for the current working directory. The user may have invoked my original program in any of the following ways (and possibly others I don't know about).

Ideally I'm looking for something which will work on a unix system and windows via MinGW.

Upvotes: 2

Views: 913

Answers (2)

dimba
dimba

Reputation: 27631

Concat getcwd() and dirname(argv[0])

Upvotes: 1

Timothy Pratley
Timothy Pratley

Reputation: 10672

http://c-faq.com/osdep/exepath.html

According to the C FAQ it can't be done reliably

Finding current executable's path without /proc/self/exe

Upvotes: 2

Related Questions