2011-04-23 2 views
1

나는 argv[0] 우리가 프로젝트 .... 을 저장 만하는 이름 즉, 프로그램 이름을 보관 유지하는 것을 읽어 내 목표 C 명령 줄 프로그램에서 다음 문을 실행할 때프로그램 이름은 [0]

const char *proName=argv[0]; 
내가 콘솔에서 다음을 참조

... 는 그래서 무엇을 주는가 ....

여기
/Users/apple/Library/Developer/Xcode/DerivedData/testaehcfzilirskhjbifrwresgppvbp/Build/Products/Debug/test 

테스트 내 프로그램의 이름입니다 전체 경로 또는 프로그램 이름은? 감사합니다.

+0

일부 관련 정보 : http://stackoverflow.com/questions/273691/using-progname-instead-of-argv0 – Mat

답변

2

당신은 특정 일을 포함 argv[0]에 의존 할 수 없습니다 말했다

는 맥 OS X의 실행 파일의 이름을 얻는 다른 방법이있다. 때때로 전체 경로, 때로는 프로그램 이름 만 얻는 경우가 있습니다.

코드가 어떻게 호출되었는지에 따라 다릅니다.

3

잘 알다시피, argv[0]은 항상 실행 파일의 전체 경로를 포함 할 수 있습니다. 파일 이름만을 원하는 경우, 하나 개의 솔루션은 다음과 같습니다

매트에 의해 언급 한 바와 같이
#include <libgen.h> 

const char *proName = basename(argv[0]); 

, argv[0] 항상 신뢰할 수 없습니다 - 그것은 일반적으로해야하지만. 그것은 당신이 성취하려고하는 것이 무엇인지에 달려 있습니다.

#include <mach-o/dyld.h> 
#include <libgen.h> 

uint32_t bufsize = 0; 

// Ask _NSGetExecutablePath() to return the buffer size 
// needed to hold the string containing the executable path 
_NSGetExecutablePath(NULL, &bufsize); 

// Allocate the string buffer and ask _NSGetExecutablePath() 
// to fill it with the executable path 
char exepath[bufsize]; 
_NSGetExecutablePath(exepath, &bufsize); 

const char *proName = basename(exepath); 
+0

execl ("/ 사용자/실행 파일", "argv [0]에 나타납니다") , "arg1", ...);'패배시킬 것입니다. 명심 해. – Mat

+0

@Mat 사실입니다. –

관련 문제