[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#define MAXSTR 5
int main(void)
{
int i;
FILE *pipe_fp;
char *strings[MAXSTR] = {"hello", "www", "groad", "net", "good"};
if ((pipe_fp = popen("sort", "w")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
for (i = 0; i < MAXSTR; i++) {
fputs(strings, pipe_fp);
fputc('\n', pipe_fp);
}
pclose(pipe_fp);
return (0);
}
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *pipe_in, *pipe_out;
char readbuf[128];
if ((pipe_in = popen("ls", "r")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
if ((pipe_out = popen("sort", "w")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
if (( pipe_out = popen("sort", "w")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
while (fgets(readbuf, 128, pipe_in))
fputs(readbuf, pipe_out);
pclose(pipe_in);
pclose(pipe_out);
return (0);
}