Thread: Program - How to get the data path

  1. #1
    Registered User
    Join Date
    Nov 2014

    Post Program - How to get the data path

    Hey everyone.
    I got a little question about getting the data path of my c program. I heard something like it's already given in the "int main(int argc, char **argv)". But with witch function can I get it?
    Do you have any suggestions?
    For example: The program is called Pathfinder and the path is 'D:\Path1\Path2\Pathfinder.exe'.
    I have no idea how to start so I'm very happy about any help.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Which witch?

    Assuming you have specified main() in the form "int main(int argc, char **argv)" form, look at argv[0] first. While it is not guaranteed to contain the complete path of your program, with a fair few implementations it does.

    Second choice is to look at the getenv() function. getenv("PATH") will usually return the contents of the PATH environment variable. Since the contents of that environment variable may contain multiple directories (separated by semi-colons) it will be necessary to extract individual directories (and check if your executable is in each one).

    Given the way you have represented you path example, I assume you are working under windows. In that case, you can use WIN API functions like GetModuleFileName() (to get the name of the executable) or GetEnvironmentVariable() (to expand an environment variable). Obviously, these options are windows specific.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by grumpy
    Which witch?
    My avatar, maybe? She specialises in C++, not C, though.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Use GetModuleFileName(), it gives you path to a program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get application data folder path
    By skuallpa in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2009, 03:19 PM
  2. global program in cmd.exe / bin? / PATH?
    By brooksbp in forum Windows Programming
    Replies: 1
    Last Post: 01-03-2007, 02:30 PM
  3. Getting the path file of your program?
    By RedZone in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2006, 11:25 AM
  4. a space in a program path
    By eam in forum C Programming
    Replies: 2
    Last Post: 10-26-2003, 10:19 PM
  5. DOS program path
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2002, 07:56 PM

Tags for this Thread