我可以在Linux终端中运行以下命令:
xwd -root | xwdtopnm | pnmtojpeg > screen.jpg
它会生成一个当前屏幕的截图。
我尝试使用下面的代码来实现同样的功能:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fpipe;
char *command="xwd -root | xwdtopnm | pnmtojpeg";
char line[256];
if ( !(fpipe = (FILE*)popen(command,"r")) )
{ // If fpipe is NULL
perror("Problems with pipe");
exit(1);
}
while ( fgets( line, sizeof line, fpipe))
{
//printf("%s", line);
puts(line);
}
pclose(fpipe);
}
我编译并运行程序./popen > screen.jpg
,但生成的文件screen.jpg无法识别。如何才能正确地通过管道传输我的程序?