Solved

how to reference the executable directory

Posted on 1998-01-06
3
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

688 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