473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Reading stdin once confuses second stdin read

Hi,

I have a program which takes the output filename argument from stdin.
Once the program knows the output filename, it tries to open it.
If the output file exists, the program asks the user to confirm
whether he really wants to overwrite the existing output file.
The problem is that the second read from stdin, to obtain the user
response whether to overwrite the existing output file, never
waits for the user's response. It's as if a bunch of characters
that are sitting in stdin get read before the user can respond.
I've tried strategically placing fflush(stdin) and fclose(stdin)
to fix the problem, but these fixes do not work.
I think there is something subtle about reading/flushing/closing
stdin that I do not understand. Any ideas?

Thanks,
Charlie

The logic looks like this:

User types...

echo "file_in" | my_program -o file_out

my_program uses fscanf to read stdin for the input filename---

while((cnv_nbr=fscanf(stdin,"%256s\n",bfr_in)) != EOF){
file_in=(char *)strdup(bfr_in);
}

my_program then opens file_in, does a lot of work (but does not
touch stdin again) until it eventually tries to write it to
file_out. If file_out already exists, user is queried and program
reads user response from stdin:

rcd=stat(file_out,&stat_sct);
if(rcd != -1){
fprintf(stdout,"File already exists---`o'verwrite (y/n)?");
usr_reply=(char)fgetc(stdin);
}

However, the fgetc() does not wait for the user to type anything.
It immediately reads a (number of) garbage characters (above ASCII
128) that the user never typed. Why?

--
Charlie Zender, su*****@uci.edu, (949) 824-2987, Department of Earth
System Science, University of California, Irvine CA 92697-3100

Nov 14 '05 #1
6 2445
On Sat, 19 Jun 2004 15:46:18 -0700, Charlie Zender <ze****@uci.edu>
wrote in comp.lang.c:
Hi,

I have a program which takes the output filename argument from stdin.
Once the program knows the output filename, it tries to open it.
If the output file exists, the program asks the user to confirm
whether he really wants to overwrite the existing output file.
The problem is that the second read from stdin, to obtain the user
response whether to overwrite the existing output file, never
waits for the user's response. It's as if a bunch of characters
that are sitting in stdin get read before the user can respond.
I've tried strategically placing fflush(stdin) and fclose(stdin)
to fix the problem, but these fixes do not work.
I think there is something subtle about reading/flushing/closing
stdin that I do not understand. Any ideas?

Thanks,
Charlie

The logic looks like this:

User types...

echo "file_in" | my_program -o file_out

my_program uses fscanf to read stdin for the input filename---

while((cnv_nbr=fscanf(stdin,"%256s\n",bfr_in)) != EOF){
file_in=(char *)strdup(bfr_in);
}
Why are you using fscanf(stdin) instead of just plain scanf()? There
is absolutely no difference in functionality at all for the extra
typing?
my_program then opens file_in, does a lot of work (but does not
touch stdin again) until it eventually tries to write it to
file_out. If file_out already exists, user is queried and program
reads user response from stdin:

rcd=stat(file_out,&stat_sct);
if(rcd != -1){
fprintf(stdout,"File already exists---`o'verwrite (y/n)?");
usr_reply=(char)fgetc(stdin);
}

However, the fgetc() does not wait for the user to type anything.
It immediately reads a (number of) garbage characters (above ASCII
128) that the user never typed. Why?


This is a FAQ, specifically
http://www.eskimo.com/~scs/C-faq/q12.18.html.

A link to the entire FAQ is in my signature.

In addition, is it never a good idea to mix input types in a C
program, especially for live user input. It is generally a good idea
to use the *scanf() family at all in this case. Some claim that it is
possible, but it is most certainly extremely difficult to use these
functions safely and correctly on arbitrary input. With or without
scanf(), however, it makes things more difficult to mix line oriented
or formatted input with single character input functions like fgetc().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Charlie Zender wrote:

I have a program which takes the output filename argument from stdin.
Once the program knows the output filename, it tries to open it.
If the output file exists, the program asks the user to confirm
whether he really wants to overwrite the existing output file.
The problem is that the second read from stdin, to obtain the user
response whether to overwrite the existing output file, never
waits for the user's response. It's as if a bunch of characters
that are sitting in stdin get read before the user can respond.
I've tried strategically placing fflush(stdin) and fclose(stdin)
to fix the problem, but these fixes do not work.
I think there is something subtle about reading/flushing/closing
stdin that I do not understand. Any ideas?
.... snip ...
my_program uses fscanf to read stdin for the input filename---


Don't use fscanf for interactive input. Use fgets. Check that
the last char in the input string is a '\n' (else there is more in
the input buffer) and change it to a '\0' for the filename.

Never use fflush on an input file. If you need to flush the stdin
input buffer (only in the 'more' case above) use a statement like:

int ch; /* declared in a suitable place */

while ((EOF != (ch = getchar())) && (ch != '\n')) continue;

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #3
Jack Klein wrote:
.... snip ...
In addition, is it never a good idea to mix input types in a C
program, especially for live user input. It is generally a good
idea to use the *scanf() family at all in this case. Some claim


I trust you omitted a 'not' in the previous sentence :-)
Alternatively, substitute 'bad' for 'good'.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
"CBFalconer" <cb********@yahoo.com> wrote:
Jack Klein wrote:

[snip]
It is generally a good idea to use the *scanf() family
at all in this case.


I trust you omitted a 'not' in the previous sentence :-)
Alternatively, substitute 'bad' for 'good'.


The phrase "at all" gives it away that Jack's intention was to negate the
word "good". Why do I seem to read so many people accidentally leaving out a
"not" or "n't" in sentences these days? Doesn't the sentence just look and
read totally wrong without it?

--
Simon.
Nov 14 '05 #5
In <gn********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
In addition, is it never a good idea to mix input types in a C
program, especially for live user input. It is generally a good idea
to use the *scanf() family at all in this case. Some claim that it is
possible, but it is most certainly extremely difficult to use these
functions safely and correctly on arbitrary input.


The actual claim is that [f]scanf is the best way of getting a line of
input, for further processing by sscanf or whatever.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <ad******************************@news.teranews.co m> "Ralmin" <ne**@ralminNOSPAM.cc> writes:
"CBFalconer" <cb********@yahoo.com> wrote:
Jack Klein wrote:

[snip]
> It is generally a good idea to use the *scanf() family
> at all in this case.


I trust you omitted a 'not' in the previous sentence :-)
Alternatively, substitute 'bad' for 'good'.


The phrase "at all" gives it away that Jack's intention was to negate the
word "good". Why do I seem to read so many people accidentally leaving out a
"not" or "n't" in sentences these days? Doesn't the sentence just look and
read totally wrong without it?


Because they write such sentences without (proof)reading them.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
6
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
8
by: orium69 | last post by:
Hi! how could i know if it was written something in stdin since the last time i read it? Tks...
24
by: Olaf \El Blanco\ | last post by:
Where is exactly neccesary to put "rewind(stdin)"? After a printf? Before a scanf? I have a lot of problems with strings... If somebody know any good document, please let me know where is... ...
3
by: joshuawilsonster | last post by:
All, I'm hoping you can provide some help. I have opened a temporary file, written to it, verified the correct amount was written, done freopen() for the file to stdin, then try read the...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
25
by: 7stud | last post by:
I can't break out of the for loop in this example: ------ import sys lst = for line in sys.stdin: lst.append(line) break
3
by: Jon Harrop | last post by:
I can read bytes from a FileStream with stream.ReadByte() but that does not handle stdin. I can read from stdin with System.Console.In using reader.Read() but it is 6x slower. So how do I read...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.