Redirection question
aquesada at mtecv2.mty.itesm.mx
aquesada at mtecv2.mty.itesm.mx
Thu Nov 15 08:12:08 AEST 1990
From: roarment at faui09.informatik.uni-erlangen.de (Roberto Armenti)
Message-ID: <roarment.658423394 at faui09>
>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 <stdio.h>
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 at mtecv2.mty.itesm.mx
More information about the Comp.lang.c
mailing list