2

我想在 C 源代码中使用 Linux 命令。

我可以使用该System()功能吗?这在Linux上可能吗?

如果我不能使用 System() 函数,我该怎么办?我想“tar xvf example.tar”。

4

2 回答 2

3

您可以使用system()exec()功能

于 2012-11-30T02:33:53.637 回答
1

If you just want to execute shell command and not looking for any return value then you can use system() function call.

Using system call is not advisable (see system man page). I would recommend you using exec() which should be invoked in child process after doing fork().

The next alternative can be using popen().

    piff = (FILE *)popen("ls -l", "r");
    if (piff == (FILE *)0)
            return (-1);
    while ((i = read(fileno(piff), buff, sizeof (buf))) == -1) {
    if (errno != EINTR) {
        break;
    }
    (void)pclose(piff);
于 2012-11-30T03:37:29.573 回答