*停權中*
加入日期: Dec 2016
文章: 23
|
問一個關於 Objective-C 與 C 的問題
例如我編譯的 xxx.app 底下有兩個文件例如 cat 與 readme.txt
我打算在yyyy/xxx.app程式啟動的時候執行 $ yyyy/xxx.app/ls -l /yyyy/xxx.app/readme.txt yyyy 是隨機數值 在 Objective-C 上該怎麼寫 在 C 上面執行shell我目前知道用 mianbundle Patch = yyyy file_ls = mianbundle Patch + "/ls" file_readme = mianbundle Patch + "/readme.txt" 代碼:
pid_t pid; char *argv[] = { file_ls, "-l", file_readme, NULL }; 但是這個 mianbundle Patch 我不知道怎麼寫 此文章於 2016-12-25 10:41 PM 被 Goemon 編輯. |
|||||||
2016-12-25, 10:40 PM
#1
|
*停權中*
加入日期: Dec 2016
文章: 23
|
那換個方式問好了
代碼:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSLog(@“\n\App Path: %@", bundlePath); NSString *exec_launch = [[NSBundle mainBundle] pathForResource:@"launch" ofType:@""]; NSLog(@"\n\app Path: %@", exec_launch); 換成 C++ 寫法 該怎麼寫 |
||
2016-12-26, 12:53 AM
#2
|
*停權中*
加入日期: May 2014 您的住址: 某個不大不小的島
文章: 628
|
不明覺厲
只好頂帖. |
2016-12-26, 06:03 AM
#3
|
*停權中*
加入日期: Dec 2016
文章: 23
|
引用:
感謝頂帖, 我寫好了. 代碼:
#include "CoreFoundation/CoreFoundation.h" // get app mainBundle CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef resourcesURL = CFBundleCopyBundleURL(mainBundle); CFStringRef str = CFURLCopyFileSystemPath( resourcesURL, kCFURLPOSIXPathStyle ); CFRelease(resourcesURL); char* app_bundle = malloc(sizeof(char) * 1024); CFStringGetCString( str, app_bundle, FILENAME_MAX, kCFStringEncodingASCII ); CFRelease(str); printf("App mainBundle: %s\n", app_bundle); // get app mainBundle .app/launch const char *file_launch = "/launch"; char *exec_launch; exec_launch = malloc(strlen(app_bundle) + strlen(file_launch) + 1); strcpy(exec_launch, app_bundle); strcat(exec_launch, file_launch); printf("exec_launch: %s\n", exec_launch); |
|
2016-12-26, 07:50 AM
#4
|