Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C源代码中使用linux命令_C_Linux - Fatal编程技术网

在C源代码中使用linux命令

在C源代码中使用linux命令,c,linux,C,Linux,我想在C源代码中使用Linux命令 我可以使用System()功能吗?这在Linux上可能吗 如果我不能使用System()函数,我应该怎么做?我想要“tar xvf example.tar”。您可以使用system()和exec()函数如果您只想执行shell命令而不寻找任何返回值,那么您可以使用system()函数调用 不建议使用系统调用(请参阅系统手册页)。我建议您使用exec(),它应该在执行fork()之后在子进程中调用 下一种选择是使用popen() 是的,您可以使用system()

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

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


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

您可以使用
system()
exec()
函数

如果您只想执行shell命令而不寻找任何返回值,那么您可以使用system()函数调用

不建议使用系统调用(请参阅系统手册页)。我建议您使用exec(),它应该在执行fork()之后在子进程中调用

下一种选择是使用popen()

是的,您可以使用
system()
popen()
fork()/exec()
命令。
    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);