Start New Discussion within our Software Development Community

Hi,

I am developing my program on a different machine but then I take the executable to a different machine to run. The program gets data over the serial port that I write to a file for later analysis. My problem is that when I run the program on the second computer I have to go and change the file folder name where the data will be saved. This is not a big deal but problem comes in if I forget to change the folder/file name, then no data is saved. I am wondring if it is possible to do

1.) Figure out the file path where the executable is sitting as soon as the program runs
2.) and then based on that file path create another folder in that location for the data file.

Let me know if all this makes sense or not. Any help will be appreciated and thanks in advance.

Several ways to do it, depending on how you run the program:

- If you're using relative paths in your program, they will always be relative to the current working directory. For example, if you always run your executable from the current working directory, you could find the right location relative to that.

- If you run the executable from some arbitrary location, argv[0] will always refer to the command that was used to run it. This means that if the current working directory were / and I wanted to run a program in my home folder, I might use something like /home/john/program . argv[0] would then contain /home/john/program (However, if I did cd; ./program , argv[0] would only contain ./program ).

- You can get the current working directory on a Unix-like system with getcwd(), and with GetCurrentDirectory() on a Windows system.

This article has been dead for over six months. Start a new discussion instead.