On Sunday 27 April 2003 19:43, Raúl Gutiérrez Segalés wrote:
On Sun, 27 Apr 2003, Anders Johansson wrote:
On Sunday 27 April 2003 18:33, Raúl Gutiérrez Segalés wrote:
Hi,
I am wondering how do I state in a C program that my input is comming from a pipe ?
For example:
ls | ./myprog
You get that on standard input, file descriptor 0
so read(stdin, buf, 100) shall do it ?
andjoh@winona:~/src> cat test.c #include <stdio.h> #include <unistd.h> int main(int argc, char argv[]){ char buf[100]; ssize_t size; size=read(0, buf, 100); if(size > 0){ buf[size]='\0'; printf("Received string: %s\n", buf); } exit(0); } andjoh@winona:~/src> gcc -o testC test.c andjoh@winona:~/src> echo foo|./testC Received string: foo andjoh@winona:~/src> Seems to work. Note that stdin is FILE *, not a file descriptor. #include <stdio.h> int main(int argc, char argv[]){ char buf[100]; size_t size; size=fread(buf, 1, 100, stdin); if(size > 0){ buf[size]='\0'; printf("Received string: %s\n", buf); } exit(0); } or using getc, or one of the other reading functions. man stdio man unlocked_stdio