Solved

how to reference the executable directory

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

785 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