I am new to this. Hope I won't break any etiquette unknowingly. I DID my homework, I believe ...

I'd like to get C/C++ code which allows me to get the path of the active application, ideally in the format of char. I wrote a program for which I want to retrieve info from a txt file (specific parameters) and also to save data in a txt file. Since I would like to use this program on a variety of Macs, I'd like it to be able to automatically retrieve the path so no matter what it'll find the parameter.txt file and the savedata.txt file.

I am using Macintosh computers with OS X (10.4.8), Metrowerk's CodeWarrior C/C++, and Carbon platform (until I learn cocoa).

Here's sample code I had hoped would do the job but it doesn't because I don't know what an "environment variable" is (i.e. how to define one, how to use it). Maybe to use getenv is totally wrong?

#include <stdio.h>
#include <stdlib.h>

int main ()
char *value;
char *var;

if( (value = getenv(var)) == NULL)
{ printf("%s is not an environmental variable", var);}
{ printf("%s = %s \n", var, value);} // I hoped that var would contain the path.

return 0;

Thanks for your patience and any help you might be able to offer!


Edited by happygeek: fixed formatting

9 Years
Discussion Span
Last Post by bploog

argv[0] parameter in main contains that info.

int main(int argc, char *argv[])
   printf("%s\n", argv[0]);
    return 0;

That was fast!!! Thanks! However, I tried what you suggested and got the following "Warning" from the debugger ... and nothing was printed out (i.e., the std window didn't open. But when I inserted a line (before your printf statement),

printf("This is the new program!\n");

it did print the quoted text to the regular standard window.

Warning : variable / argument 'argc' is not used in function
HelloWorld.cp line 4 int main (int argc, char *argv[])


You need to invoke the program from the command line using the executable created after the compilation process to work.


Regarding the compiler: why are you still using CodeWarrior? Apple's Xcode application is a free download, is of commercial quality, and provides roughly the same functionality (and more) of CodeWarrior.

Also, I believe that using Xcode's Build & Run button actually invokes the application from the command line, eliminating the problem you are experiencing right now.


You are right, of course. The problem is that I am used to CW and that I haven't had a chance to invest the time to switch. I am also not quite sure how to make the switch. I have been looking for an Intro book for XCode. The Apple documentation seemed intimidating to me. If you can recommend a good starting point for the switch, I'd really appreciate it. -- BP


Thanks for all your (plural -- several helpful replies!) help. I think I solved the problem. Perhaps not the most elegant way but it seems to work for my purposes. (And I feel encouraged to switch to XCode.) -- BP


#include <unistd.h>
#include <iostream>

#define SIZE 1200

int main()

    using namespace std;
    char    folder[SIZE];
    getcwd(folder, SIZE);
    cout << "This is the path = " << folder << endl;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.