fscanf(stderr,"%d",&i); ?!?!?

Illya Vaes vaes at cs.ruu.nl
Tue Apr 23 23:15:56 AEST 1991


In <1991Apr23.075320.9473 at en.ecn.purdue.edu> tenka at en.ecn.purdue.edu (Andy Tenka) writes:

>I am writing a program which will take its input from
>both stdin (redirected from a file) and the keyboard.
>In desperation to get the input from keyboard, I used:
>
>	fscanf(stderr,"%d",&i);
>
>However, the fscanf does not wait for input from keyboard.
>It just reads 0 from stderr before I even type anything.
>I know I did something wrong here.  Could any of you gurus
>point out my mistakes and also, could you show me a way to
>write a program that takes input from both redirected stdin
>and keyboard?  Many thanks in advance.
>
>Andy

As far as I know, stderr is only for output, so you cannot read
from it. Yo probably best look on your terminal as two seperate
devices: one for giving input to the system and one for showing
output of the system to the user. Stderr uses the last one.
If you want to read from the keyboard as well as from a file, 
the most obvious thing to do is leaving the standard input intact
and opening the file to read from (with open(2)), using its file
descriptor in fscanf:
	 
	/* watch the #includes */

	FILE *file;

	file= fopen ("myfile", "r");	/* open "myfile" for reading */

	read_chars= fscanf (file, "%d", &i);
	if (read_chars ...) {
	}
	else {
	}

	... = fscanf (stdin, "...", ...);
	... = scanf ("...", ...);	/* equivalent to fscanf(stdin...) */

Hope to have helped,

Illya.

-- 
+---------------------------------------------------------------------+
|  vaes at cs.ruu.nl  UNTIL 31 May 1991.				      |
|  								      |
|  "I think, therefore I am not what I think I am" (Me, Myself and I) |



More information about the Comp.lang.c mailing list