Solved

how to reference the executable directory

Posted on 1998-01-06
3
189 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 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

16 Experts available now in Live!

Get 1:1 Help Now