当前位置: 动力学知识库 > 问答 > 编程问答 >

terminal - c programming how to convert system output to char

问题描述:

This question already has an answer here:

  • How to execute a command and get output of command within C++ using POSIX?

    7 answers

网友答案:

If everything you want is to read an environment variable in a POSIX environment, you can simply call getenv: http://man7.org/linux/man-pages/man3/getenv.3.html

#include <stdlib.h>
#include <stdio.h>

int main() {
        char* username = getenv("USER");
        printf("username = %s\n", username);
        return 0;
}

If you want something more complex, you can use popen: http://man7.org/linux/man-pages/man3/popen.3.html to create a pipe to a process and read from stdout, this answer should help in that case: C: Run a System Command and Get Output?

网友答案:

This should do the trick. I haven't tested it myself, but I checked and its listed in the Linux man pages so I'm sure its kosher.

分享给朋友:
您可能感兴趣的文章:
随机阅读: