| | |
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:
Solved Threads: 0
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
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
argv[0] parameter in main contains that info.
C++ Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { printf("%s\n", argv[0]); return 0; }
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.
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
argv[0] parameter in main contains that info.
C++ Syntax (Toggle Plain Text)
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[])
[I just used their HelloWorld.cp to only test this code separately.]
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
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
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
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
===========================
===========================
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by ~s.o.s~; Mar 2nd, 2007 at 7:34 am. Reason: Added code tags, learn to use them.
![]() |
Similar Threads
- FindNextFile() problem (C++)
- Python path help 2 (Python)
- Working with array of files (C++)
Other Threads in the C++ Forum
- Previous Thread: Query regarding the 'this' operator or pointer?
- Next Thread: Easy question: Decimals to time
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






