#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAX_BUFFER_LENGTH 1024*10
int main(int argc, char *argv[])
{
char buf[ MAX_BUFFER_LENGTH ];
int length = 0;
if( (length = read( 0, buf, MAX_BUFFER_LENGTH )) < 0 ) {
return -1;
}
buf[length] = '\0';
printf("input: \n%s\n", buf);
return 0;
}
test:
a='Hello world!'
echo $a | ./a.out
也可以通过python脚本来调用:
#!/usr/bin/python
import subprocess
input_data = 'Hello world!'
a = subprocess.Popen('./a.out', stdin = subprocess.PIPE, stdout =
subprocess.PIPE)
a.stdin.write(input_data)
output = a.stdout.read()
print output