473,625 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DEVCPP + rewind(stdin)

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.
May 24 '06 #1
24 3401
Olaf "El Blanco" wrote:
Where is exactly neccesary to put "rewind(stdin)" ?
After a printf? Before a scanf?
I've never needed to put it anywhere. I have, on the other hand, needed
to use fflush(stdout) before waiting for input.
I have a lot of problems with strings...
If somebody know any good document, please let me know where is...


The comp.lang.c FAQ http://c-faq.com/
K&R2 (see the FAQ for the full reference).
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 24 '06 #2
Olaf "El Blanco" said:
Where is exactly neccesary to put "rewind(stdin)" ?
I've never done that in my life.

Think about what it would mean.

puts("Dear user: I know you have just spent the last twenty");
puts("minutes typing in all that stuff, and I thank you for");
puts("it - but now I'd like you to do it all again, EXACTLY");
puts("THE SAME as you did it last time.");

rewind(stdin);

I don't see that program lasting through to a second run.
After a printf? Before a scanf?
Neither.
I have a lot of problems with strings...
If somebody know any good document, please let me know where is...


The best way to capture text input is either a single character at a time,
ONLY, or a line at a time, ONLY. One or the other, and which one you choose
depends on what you're doing.

I've put a little essay together on the subject of capturing text data a
line at a time, and you can find it at:

<http://www.cpax.org.uk/prg/writings/fgetdata.php>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 24 '06 #3
Richard Heathfield <in*****@invali d.invalid> writes:
Olaf "El Blanco" said:
Where is exactly neccesary to put "rewind(stdin)" ?


I've never done that in my life.

Think about what it would mean.

puts("Dear user: I know you have just spent the last twenty");
puts("minutes typing in all that stuff, and I thank you for");
puts("it - but now I'd like you to do it all again, EXACTLY");
puts("THE SAME as you did it last time.");

rewind(stdin);

I don't see that program lasting through to a second run.

[...]

If stdin is coming from an interactive device, rewind()ing it doesn't
make much sense. But if it's coming from, say, a disk file, then
rewind() *might* be a sensible thing to do.

However, the source from which stdin receives its input is generally
outside the control of the program. The rewind() function has its
uses, but if you're going to need to rewind() something, it should
probably be a named file (opened with fopen()), not stdin.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #4
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
However, the source from which stdin receives its input is generally
outside the control of the program. The rewind() function has its
uses, but if you're going to need to rewind() something, it should
probably be a named file (opened with fopen()), not stdin.


I find this unconvincing. The program has no control over whether
stdin is rewindable, but equally it has no control over whether stdin
is a file containing the right data, or even data in the right format.
It has no control over whether it's a file that it makes sense to open
in text mode. And on the other hand on many modern operating systems
it has no control over whether a named file is rewindable, especially
if the file name is not built in to the program. About the only way
to be sure a file is rewindable is for the program to create it
itself!

If you want to make your program robust against non-rewindable (or
non-fseekable) files, whether named or not, you need to use some
mechanism outside the C standard. For example in unix you might
test a file with stat() and copy it to a temporary file if necessary.

-- Richard
May 24 '06 #5
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
However, the source from which stdin receives its input is generally
outside the control of the program. The rewind() function has its
uses, but if you're going to need to rewind() something, it should
probably be a named file (opened with fopen()), not stdin.


I find this unconvincing. The program has no control over whether
stdin is rewindable, but equally it has no control over whether stdin
is a file containing the right data, or even data in the right format.
It has no control over whether it's a file that it makes sense to open
in text mode. And on the other hand on many modern operating systems
it has no control over whether a named file is rewindable, especially
if the file name is not built in to the program. About the only way
to be sure a file is rewindable is for the program to create it
itself!

If you want to make your program robust against non-rewindable (or
non-fseekable) files, whether named or not, you need to use some
mechanism outside the C standard. For example in unix you might
test a file with stat() and copy it to a temporary file if necessary.


I suppose it's a matter of style. There's nothing illegal about
rewind(stdin), but in practice it's more likely to fail than
rewind(some_fil e_I_opened_expl icitly).

I would consider it poor style to attempt to call rewind(stdin). In
normal usage, my expectation is that a program will read its standard
input from beginning to end, and will not attempt to do any
re-positioning.

I was about to mention that you should, of course, always check
whether rewind() was successful -- but I see that you can't do that.
rewind(stream) is equivalent to (void)fseek(str eam, 0L, SEEK_SET),
except that it clears the error indicator for the stream. In other
words, it deliberately ignores any errors.

Why was rewind() defined this way?

Based on that, I think I'd avoid using rewind() at all.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #6
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
In
normal usage, my expectation is that a program will read its standard
input from beginning to end, and will not attempt to do any
re-positioning.


I suppose expectations vary :-) I regard standard input as a natural
way to provide a program's primary input, regardless of how it is used
(I usually follow the unix convention of using stdin if no file name
is given on the command line).

Rewinding standard input in C has a long history. Here is an amusing
example predating the modern standard i/o library:

http://minnie.tuhs.org/UnixTree/V5/u...s1/goto.c.html

-- Richard
May 24 '06 #7
Keith Thompson wrote:
Richard Heathfield <in*****@invali d.invalid> writes:
Olaf "El Blanco" said:
Where is exactly neccesary to put "rewind(stdin)" ?


I've never done that in my life.

Think about what it would mean.

puts("Dear user: I know you have just spent the last twenty");
puts("minutes typing in all that stuff, and I thank you for");
puts("it - but now I'd like you to do it all again, EXACTLY");
puts("THE SAME as you did it last time.");

rewind(stdin);

I don't see that program lasting through to a second run.

[...]

If stdin is coming from an interactive device, rewind()ing it
doesn't make much sense. But if it's coming from, say, a disk
file, then rewind() *might* be a sensible thing to do.

However, the source from which stdin receives its input is
generally outside the control of the program. The rewind()
function has its uses, but if you're going to need to rewind()
something, it should probably be a named file (opened with
fopen()), not stdin.


And if the systems works the way I think it should work, then
rewind will fail when stdin is interactive, and work when stdin has
been redirected to a disk file.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
May 24 '06 #8
CBFalconer <cb********@yah oo.com> writes:
Keith Thompson wrote:

[...]
If stdin is coming from an interactive device, rewind()ing it
doesn't make much sense. But if it's coming from, say, a disk
file, then rewind() *might* be a sensible thing to do.

However, the source from which stdin receives its input is
generally outside the control of the program. The rewind()
function has its uses, but if you're going to need to rewind()
something, it should probably be a named file (opened with
fopen()), not stdin.


And if the systems works the way I think it should work, then
rewind will fail when stdin is interactive, and work when stdin has
been redirected to a disk file.


Except that rewind() has no mechanism for telling you that it failed.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #9
On 2006-05-24, Keith Thompson <ks***@mib.or g> wrote:
Richard Heathfield <in*****@invali d.invalid> writes:
Olaf "El Blanco" said:
Where is exactly neccesary to put "rewind(stdin)" ?


I've never done that in my life.

Think about what it would mean.

puts("Dear user: I know you have just spent the last twenty");
puts("minutes typing in all that stuff, and I thank you for");
puts("it - but now I'd like you to do it all again, EXACTLY");
puts("THE SAME as you did it last time.");

rewind(stdin);

I don't see that program lasting through to a second run.

[...]

If stdin is coming from an interactive device, rewind()ing it doesn't
make much sense. But if it's coming from, say, a disk file, then
rewind() *might* be a sensible thing to do.

However, the source from which stdin receives its input is generally
outside the control of the program. The rewind() function has its
uses, but if you're going to need to rewind() something, it should
probably be a named file (opened with fopen()), not stdin.


You could attempt to rewind (or seek) and complain loudly when it fails.
May 25 '06 #10

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

Similar topics

2
10223
by: gc | last post by:
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?
3
2670
by: Josh Wilson | last post by:
OK, somewhat new to C, have searched the group and haven't found an answer that has fixed my problem, so I am just going to try posting it. I am taking really large image files(128x128x35) and turning them into float, short, double, etc. files, writing them to an array in sections (so that the machine is working w/ smaller arrays), changing or evaluating the pixel values, and them writing them out. Doing this before, I could only find...
3
2338
by: Mike | last post by:
I am writing this program that first asks the user to type today's exchange rate between U.S. dollars and Japanese yen, then reads U.S. dollar value and converts each. I am attemtping to use 0 or negative input as sentinel. Any ideas? #include <stdio.h> #define DRAINO rewind(stdin);fflush(stdout); #define STOP rewind(stdin); getchar();
15
3999
by: ais523 | last post by:
I was just wondering whether there was a portable way to use gets() safely, and came up with this: #include <stdio.h> #include <stdlib.h> int main() { FILE* temp; char buf;
83
7799
by: deppy_3 | last post by:
Hi.I am started learning Programm language C before some time.I am trying to make a programm about a very simple "sell shop".This programm hasn't got any compile problem but when i run it i face some other ploblems which i can not correct.I would appreciated if someone take a look at my programm so as to help me.I tried many times to find out where my mistakes are but i didn't manage something. I have made some comments due to the programm...
1
3628
by: goldenllama | last post by:
Hi, thanks in advance for the assistance. I need to make a program that will take a string using gets() (I know gets() is bad, but it's what I'm supposed to use), and then print the message in reverse. One problem I had was that when I used gets(message);, the program just continued without letting me enter anything. I'm using while(strlen(gets(message)) == 0); right now and it seems to work, but I'd prefer to use gets(message); if I can. ...
1
2995
by: amhjones | last post by:
Hi, I am trying to get an employee database to work correctly, I thought that all the coding was correct but everytime I run my code i get a return of -1, help would be appreciated: #include <stdio.h> typedef struct Employee { char fname; char lname; char sub_taken; char last_edu;
22
2388
cat
by: Jag | last post by:
I've read parts of K&R's ANSI C v2 and this is what their cat looked like but when I compared the speed of this code to gnu cat, it seems very slow. How do I optimize this for greater speeds? is there an alternative algorithm? void catfile(FILE *in, FILE *out) { register int num_char; /*Get characters*/ while ((num_char = getc(in)) != EOF) {
4
212
by: Robert Blass | last post by:
I am looking to get my feet wet with encryption. When I say encryption program I am talking about something to get me off to a quick start. Something very simple, far less than the 40+ bit encryption code. What I need is an easy to understand language choice. I've used BASIC a long time ago but is there another language that is better and more supported? There seems to be hundreds of languages so it's hard for me to just pick one.
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
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
7178
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
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.