How to get path of current appl (in char) for Mac OS X

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 7
Reputation: bploog is an unknown quantity at this point 
Solved Threads: 0
bploog bploog is offline Offline
Newbie Poster

How to get path of current appl (in char) for Mac OS X

 
0
  #1
Mar 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #2
Mar 1st, 2007
argv[0] parameter in main contains that info.
  1. int main(int argc, char *argv[])
  2. {
  3. printf("%s\n", argv[0]);
  4. return 0;
  5. }
Last edited by Ancient Dragon; Mar 1st, 2007 at 9:32 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 7
Reputation: bploog is an unknown quantity at this point 
Solved Threads: 0
bploog bploog is offline Offline
Newbie Poster

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #3
Mar 1st, 2007
Originally Posted by Ancient Dragon View Post
argv[0] parameter in main contains that info.
  1. int main(int argc, char *argv[])
  2. {
  3. printf("%s\n", argv[0]);
  4. return 0;
  5. }
*******
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[])

[I just used their HelloWorld.cp to only test this code separately.]
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #4
Mar 1st, 2007
You need to invoke the program from the command line using the executable created after the compilation process to work.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #5
Mar 1st, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 7
Reputation: bploog is an unknown quantity at this point 
Solved Threads: 0
bploog bploog is offline Offline
Newbie Poster

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #6
Mar 2nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 7
Reputation: bploog is an unknown quantity at this point 
Solved Threads: 0
bploog bploog is offline Offline
Newbie Poster

Re: How to get path of current appl (in char) for Mac OS X

 
0
  #7
Mar 2nd, 2007
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

===========================
  1. #include <unistd.h>
  2. #include <iostream>
  3.  
  4. #define SIZE 1200
  5.  
  6. int main()
  7. {
  8.  
  9. using namespace std;
  10.  
  11. char folder[SIZE];
  12.  
  13. getcwd(folder, SIZE);
  14.  
  15. cout << "This is the path = " << folder << endl;
  16. }
Last edited by ~s.o.s~; Mar 2nd, 2007 at 7:34 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC