[net.micro.pc] strange stdin behavior

shdanfor@ut-sally.UUCP (Scott Danforth) (06/13/84)

I'm using Microsoft C on a COMPAQ.

Can anybody tell me why the following C program to copy stdin to stdout
doesn't work.  (If I do "cpstd < file", each line of file is printed twice.)


#include <stdio.h>
main()
{
    int c;
    while ((c = getc(stdin)) != EOF)
	putc(c,stdout);
}

dag@tellab2.UUCP (Donald Graft) (06/14/84)

The program is not performing incorrectly.  Most micro-based C compilers that
I am familiar with treat getc(stdin) as equivalent to getchar().  That means
that characters will be echoed to the console as typed so the user can see
what (s)he's doing.  Since the characters are also printed by the putc(c)
call, the double printing is accounted for.  You need to find a call that
gets characters without echoing.  Most libraries provide such a call; if
not, you will need to make a direct OS call.

...ihnp4!tellab1!tellab2!dag         Donald Graft

jcw@cvl.UUCP (06/14/84)

>From: shdanfor@ut-sally.UUCP
>
>I'm using Microsoft C on a COMPAQ.
>Can anybody tell me why the following C program to copy stdin to stdout
>doesn't work.  (If I do "cpstd < file", each line of file is printed twice.)
>
>#include <stdio.h>
>main()
>{
>    int c;
>    while ((c = getc(stdin)) != EOF)
>	putc(c,stdout);
>}

The stdio routines for your C compiler probably use the buffered
keyboard input DOS function call (INT 13?).  Even if the input is being
redirected, this function call echoes the lines to the screen.  Your
routine also writes to the screen, so that's why you get them printed
twice.  If you use a function call that does not echo, then when stdin
is not redirected it is confusing to type.  A solution would be to use
IOCTL to determine if stdin is the console, and use the appropriate
function call.  I'm not sure if the DOS 2.0 io calls are smarter; I'll
try it out.

Jay Weber
..!seismo!rlgvax!cvl!jcw
..!seismo!rochester!jay
jay@rochester.arpa

geller@rlgvax.UUCP (David Geller) (06/15/84)

LATTICE "C" also provides some DIRECT CONSOLE i/o functions. These
functions provided RAW or unbuffered i/o with the terminal. Their
names are:
			c=getch();	int	c;
			putch(c);
			r=ungetch(c)	int	r;	char	c;
			p=cgets(s);	char	*p,*s;
			cputs(s);	char	*s;
			cscanf/cprintf
They are all VERY useful.
					David P. Geller
					Computer Consoles, Inc.
	{seismo}!rlgvax!geller		Office Systems Group
					11490 Commerce Park Drive
					Reston, VA  22091
					703-648-3483