我試圖編程調用「頂部」命令。下面是使用的代碼:什麼導致此命令行錯誤?
char buffer [128];
char* threadsPointer;
char* procPointer;
NSString* numberOfThreadsString;
NSString* numberOfProcString;
FILE* output = popen("/usr/bin/top", "r");
while (fgets(buffer, sizeof(buffer), output) != NULL)
{
if ((procPointer = strstr(buffer, "Processes:")) != NULL)
{
procPointer += strlen("Proceses: ");
strcpy(buffer, procPointer);
numberOfProcString = [NSString stringWithUTF8String: buffer];
}
if ((threadsPointer = strstr(buffer, "sleeping,")) != NULL)
{
threadsPointer += strlen("sleeping, ");
strcpy(buffer, threadsPointer);
numberOfThreadsString = [NSString stringWithUTF8String: buffer];
}
}
NSLog(@"Proc: %@\nThreads: %@\n\n\n", numberOfProcString, numberOfThreadsString);
而不是給有效的輸出,我不斷收到錯誤:「錯誤打開終端:未知」。我讚揚了整個代碼來識別問題,並意識到它的行:FILE* output = popen ("/usr/bin/top", "r");
這是導致錯誤。
有沒有人有我做錯什麼的想法?注意我在Mountain Lion OS X上爲OSX構建一個不是iOS的應用程序。
ps不提供我需要的信息 - 正在運行的線程和進程的總數。頂部是我唯一的選擇。我如何將標準輸出連接到有效的終端? – fdh