popen.c

来自「基于网络编程的例子」· C语言 代码 · 共 38 行

C
38
字号
/* * popen.c - Using popen() to open a pipe */#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <limits.h>#define BUFSZ PIPE_BUFvoid err_quit(char *msg);int main(void){    FILE *fp; /* FILE stream for popen */    char *cmdstring = "cat popen.c";    char buf[BUFSZ]; /* Buffer for "input" */    /* Create the pipe */    if((fp = popen(cmdstring, "r")) == NULL)        err_quit("popen");    /* Read cmdstring's output */    while((fgets(buf, BUFSZ, fp)) != NULL)        printf("%s", buf);    /* Close and reap the exit status */    pclose(fp);    exit(EXIT_SUCCESS);}void err_quit(char *msg){    perror(msg);	exit(EXIT_FAILURE);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?