美文网首页
oc 调终端命令返回结果

oc 调终端命令返回结果

作者: 皮蛋豆腐酱油 | 来源:发表于2019-07-25 12:34 被阅读0次
- (NSString *)executeCommand: (NSString *)cmd {
    NSString *output = [NSString string];
    FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r+");
    if (!pipe) {
        return @"";
    }
    char buf[102400];
    while(fgets(buf, sizeof(buf), pipe) != NULL) {
//        if('\n' == buf[strlen(buf)-1]) {
//            buf[strlen(buf)-1] = '\0';
//        }
        output = [output stringByAppendingFormat: @"%s ", buf];
    }
  
    pclose(pipe);
    return output;
    
}

//这个也可以参考一下
FILE *fp = popen("ps -eo start,user,pid,pcpu,vsz,rss,etime,utime,stime,msgsnd,msgrcv", "r"); 

相关文章

网友评论

      本文标题:oc 调终端命令返回结果

      本文链接:https://www.haomeiwen.com/subject/ygzcrctx.html