Hello,

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);}
else
{ 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!

BP

Dani AI

Generated

Brief expert note tying the replies together and a couple of safer alternatives.

's argv[0] and 's getcwd are useful quick fixes for command‑line runs (and correctly pointed out that launching from a shell behaves differently than Finder). However, both approaches are fragile for GUI apps: Finder often sets an unexpected working directory and argv[0] can be relative or a symlink. For macOS the reliable choices are (A) use the app bundle APIs to find bundled resources, or (B) for standalone CLI tools use the dyld API and then canonicalize the path.

A — bundle-aware (recommended for GUI apps): use Core Foundation/Bundle calls to get the bundle folder or a resource file and convert the CFURL to a C string. This returns the actual .app or resource path (safe when the app is launched from Finder):

#include <CoreFoundation/CoreFoundation.h>
#include <limits.h>

bool GetBundlePath(char *out, size_t maxLen) {
    CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    if (!url) return false;
    bool ok = CFURLGetFileSystemRepresentation(url, true, (UInt8*)out, (CFIndex)maxLen);
    CFRelease(url);
    return ok;
}

/* CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("parameter"), CFSTR("txt"), NULL)
   + CFURLGetFileSystemRepresentation gives the full resource filename as a C path. */

B — non‑bundled executable: use _NSGetExecutablePath and then realpath() to resolve symlinks to an absolute path:

#include <mach-o/dyld.h>
#include <limits.h>
#include <stdlib.h>

char exe[PATH_MAX];
uint32_t sz = sizeof(exe);
if (_NSGetExecutablePath(exe, &sz) == 0) {
    char real[PATH_MAX];
    if (realpath(exe, real)) { /* real is absolute path to executable */ }
}

Practical tips: bundle resources via Xcode (easier to ship), do not write mutable data into the .app bundle — use the per‑user Application Support folder instead, always check buffer sizes/return values, and prefer CFURLGetFileSystemRepresentation or NSString::UTF8String for safe C strings (handles encoding). These approaches are robust across launches from Finder, shell, and launchd.

Recommended Answers

All 6 Replies

argv[0] parameter in main contains that info.

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

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
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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.