0

I have a program defined in a certain directory A, and a shortcut to it in another directory B, and I have B added to PATH. In a different directory C I am running cmd.exe and am executing this program with the command progname.lnk arg1 arg2. My understanding is that the "current working directory" in this situation is C, but when I use _getcwd (see below) to obtain the current working directory I get directory A, the place where the program is kept.

#include <iostream>
#include <direct.h>
#define GCWD _getcwd

int main() {
  char cwd[256];
  GCWD(cwd, 256);
  std::cout << cwd << std::endl;
}

I compiled and ran this program with the command mwe.lnk. This program is stored in A, has a shortcut to it in B, and I ran it from C, just like my actual program.

This code is informed by computinglife's answer to a related question, which is the same technique used here.

Is my understanding of what a "current working directory" is correct? It seems to agree with the first paragraph of computinglife's answer, so what am I doing wrong?

Mushroom Man
  • 400
  • 3
  • 18
  • 1
    Windows links could have a property to set the working directory of the program it runs, please take a look at that (right-click on link, and properties). – Some programmer dude Apr 02 '20 at 21:35
  • This worked, thank you! I shall add an answer for this. – Mushroom Man Apr 02 '20 at 21:42
  • There is no conceivable reason, ever, to use the current working directory. For anything. What problem are you trying to solve where the current working directory is deemed to be part of the solution? – IInspectable Apr 03 '20 at 07:08
  • I made a program which combines multiple images (which are supposed to be scans of approximately letter-size paper) into a single PDF using LaTeX. The user (just me) enters the directory where these images are located and types a command like `scanstex.lnk page1 page2`, and the program puts the output file in the current working directory. – Mushroom Man Apr 03 '20 at 22:09

1 Answers1

0

When creating shortcuts Windows configures them to set the working directory to the location where the program is stored. Emptying this field in the shortcut properties solved the problem.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mushroom Man
  • 400
  • 3
  • 18