473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rewinding stdin

gc
I was having trouble getting fgets to read a string from stdin, it was
reading a '\n' already in the buffer.
Someone told me to rewind stdin before calling fgets. It works, but is
it permissible?
Nov 13 '05 #1
2 10222
gu**********@ya hoo.com (gc) wrote:
I was having trouble getting fgets to read a string from stdin, it was
reading a '\n' already in the buffer.
This is usually caused by mixing calls to *scanf() and fgets(). After
a call to *scanf() it's good practice to drain the input puffer up to
the next '\n'. [ Just in case: no, you cannot use fflush(stdin),
fflush() works for output streams only! ]
Someone told me to rewind stdin before calling fgets. It works, but is
it permissible?


Huh? Well, this is possible if, and only if, stdin is associated with a
RealFile(tm): rewind() sets the file position indicator for the stream
to the beginning of the file - you will start off once again. And, of
course, rewind() won't work if stdin is associated with an interactive
device - or do you know a way to rewind your keyboard? ;)

As mentioned above: it is advisable to drain the input buffer after
calls to *scanf() in order to resync your input stream before subsequent
read operations.

Example:

int c;
while ( ( c = fgetc( stdin ) != EOF ) && ( c != '\n' ) )
/* empty loop body */;
Addition:

If you want to push back one character into the input stream, you
may make use of the ungetc function. But be aware that only _one_
character of pushback is guaranteed by the standard.

Regards

Irrwahn
--
6 * 9 = 42 (base 13)
Nov 13 '05 #2
Irrwahn Grausewitz <ir*****@freene t.de> wrote:

<SNIP>
Ooops...
Works better when parantheses match:
while ( ( c = fgetc( stdin ) != EOF ) && ( c != '\n' ) )


while ( ( ( c = fgetc( stdin ) ) != EOF ) && ( c != '\n' ) )

--
6 * 9 = 42 (base 13)
Nov 13 '05 #3

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

Similar topics

11
3483
by: Thomas Matthews | last post by:
Hi, I've searched the FAQ and no tips on this one. Searching the newsgroups shows that the proper method for rewinding an istream is: istream inp; // read past EOF. inp.clear(); // Clear the stream state inp.seekg(0);
0
1821
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with an EOF. After some debugging the reason for this appears to be that the stdin and stdout that the ReadLine function in the tokeniser include are not affected by my redirection and refer to a non-terminal so don't stall waiting for input from my...
3
18368
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second program to read the characters the first program has written to stdin, but I don't get it how to do this. The program which writes to stdin:
23
7722
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
6
2475
by: Charlie Zender | last post by:
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...
6
2218
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and library input prompts will not happen if stdin is supplied by pipe or redirection. So, I would like to include a statement in the pluggin
1
7470
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
24
3401
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... Thank you.
0
1991
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With Python it's easy, use the ps1 prompt. --- begin --- import sys
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.