如何使用execl运行管道并将结果写入文件?

时间:2020-10-20 17:14:37

标签: c bash execl

我尝试在C语言中执行类似的操作,但是我不知道如何:(:

$: xwd -silent -root | convert xwd:- -crop 1920x1080+0+0 test.png

使用execl。

现在我只能做这样的事情:

      #include <unistd.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <stdio.h>
      
      int main(int argc, char *argv)
      {
          char *binaryPath = "/usr/bin/xwd";
          char *arg1 = "-root";
          char *arg2 = "-silent";
      
          int fd; /*file descriptor to the file we will redirect ls's output*/
      
          if((fd = open("xwd1.xwd", O_RDWR | O_CREAT , 0666))==-1){ /*open the file */
              perror("open");
              return 1;
          }
      
          dup2(fd,STDOUT_FILENO); 
          dup2(fd,STDERR_FILENO); 
          close(fd); 
          execl( binaryPath , binaryPath ,arg1,arg2, (char *) 0 );
      
          return 0;
      }

它将屏幕转储写入文件xwd1.xwd,但是我不知道如何使用convert制作管道。 感谢您提供的所有信息和帮助。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题:)。 我做了类似的事情:

 system("xwd -silent -root | convert xwd:- -crop 1920x1080+0+0 test.tiff");

感谢您的所有评论 克里斯

相关问题