Solved

how to reference the executable directory

Posted on 1998-01-06
3
187 Views
Last Modified: 2010-04-10
I have this program, sitting on Netware k:\abc\.  When it runs, it needs to read in a file from the directory where the execuatable resides (so k:\abc\).
 
When a LAN user execute this program from their desk top, at c:\, they type -
k:\abc\theExe

How does theExe know to search the executable's directory instead of user's c:\?  I know the following will NOT work -
FILE* = fopen("file.txt", "r");   //this look into the current working directory of the user - c:\

I know I can specify the \\server\path in my program.  Is there any other ways?
0
Comment
Question by:closingcost
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1177670
From a command line program (which I assume you are talking about since you said the user would type k:\abc\theExe) you can get the command used to invoke the program by referencing argv[0].  You can then strip off the "theExe" part and the remainder will be your invokation directory.  For example:

void main(int argc, char *argv[])
{
  if(argc > 0){
    printf("The executable for this file is: %s\n", argv[0]);
  }
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177671
This is true for non-command line programs as well.  It works for Windows programs and (I believe--its been 10 years) for Mac as well.
0
 

Author Comment

by:closingcost
ID: 1177672
jhance,

I have tried the argv[0].  Seems that if I execuate from a different directory other than the one theEXE resides, and I put the theEXE's path in $PATH, argv[0] came out = what I typed at the command line, ie theEXE.  The full path is not displayed. Is there a MFC function to get the path of the executable?

I'm writing code in VC++ 5.0.  Program will be execuated by user from both NT/Win95 machine.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now