Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!helios!mtecv2!root From: aquesada@mtecv2.mty.itesm.mx Newsgroups: comp.lang.c Subject: Re: Redirection question Message-ID: <2683@mtecv2.mty.itesm.mx> Date: 14 Nov 90 21:12:08 GMT References: Sender: root@mtecv2.mty.itesm.mx Reply-To: aquesada@mtecv2.mty.itesm.mx Lines: 56 From: roarment@faui09.informatik.uni-erlangen.de (Roberto Armenti) Message-ID: >Hi net-people, > > perhaps someone out there can help me. > > Is there a _portable_ way to recognize whether input comes from > stdin or from another stream ? > To make the problem clearer : i'm working on a programm that takes > its input from stdin. If however someone chooses to redirect the input > to a file, so that data is read from that file, how can I figure that > out ( Prompting and so on should not occur in this case ) ??? > > The solution should at least work on UNIX _AND_ MS-DOS, so i'm looking for > a _portable_ implementation. > Help out there ? > Thanks in advance > Roberto > > > > > Try this, it ran under both MSDOS and Ultrix. Sorry i don't give a longer explanation but i'm in a rush. ************************************ #include FILE *entrada={stdin}; char linea[255]; main(argc,argv) char *argv[]; { if(argc>1) { if((entrada=fopen(argv[1],"r"))==NULL) puts("file not found"),exit(1); printf("Reading from file: %s\n",argv[1]); /* In here goes any other code you need to execute when reading from a file */ } while(!feof(entrada)) { fgets(linea,255,entrada); puts(linea); } } ************************************* From Monterrey, Mexico: Antonio Quesada Duarte aquesada@mtecv2.mty.itesm.mx