0

Lets say I have a folder called /folder that looks like this:

folder
|--test.txt
|--test.cpp

All test.cpp does is print the contents of the (relatively) local ./test.txt. So if I compile test.cpp to get test.exe. Then I can call:

C:/.../folder>test

and the content of test.txt will be printed to the console.

However, if I add /folder to my PATH variable, I can call the test command from anywhere on my command line. Unfortunately, the program can no longer find the test.txt file. How can I fix this without using an absolute path? I'm working on a command line tool to aid in development.

Sam McC
  • 261
  • 1
  • 3
  • 7
  • the PATH is used for finding executable files, not data files - you need to provide the path to the data file either via a command line parameter or a configuration file with a known location. –  Feb 12 '19 at 19:16

1 Answers1

0

In general, it's a bad idea to assume that "test.txt" lives in the same folder as the executable. It is better to pass it as a command line argument.

test.exe "<path to folder>/test.txt"

or

test "<path to folder>/test.txt"
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • This is a general observation and not an answer to the question as present. There is nothing wrong in principle to have a text file as a part of the application package and put it side-by-side with the executable. Not a Linux way, but a Windows way for certain. – SergeyA Feb 12 '19 at 19:56
  • @SergeyA, I beg to differ. From the OP: *How can I fix this without using an absolute path?* One solution is to provide that as a command line argument. – R Sahu Feb 12 '19 at 20:06