Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!jarthur!uunet!mcsun!hp4nl!alchemy!vaes From: vaes@cs.ruu.nl (Illya Vaes) Newsgroups: comp.lang.c Subject: Re: fscanf(stderr,"%d",&i); ?!?!? Message-ID: <1991Apr23.131556.24431@cs.ruu.nl> Date: 23 Apr 91 13:15:56 GMT References: <1991Apr2.083400.5511@athena.mit.edu> <1991Apr23.075320.9473@en.ecn.purdue.edu> Organization: Utrecht University, Dept. of Computer Science Lines: 50 In <1991Apr23.075320.9473@en.ecn.purdue.edu> tenka@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@cs.ruu.nl UNTIL 31 May 1991. | | | | "I think, therefore I am not what I think I am" (Me, Myself and I) |