Where is error checking on fgets() addressed in FAQ?

  • Thread starter William L. Bahn
  • Start date
W

William L. Bahn

I recently challenged one of my students to figure out a way to determine if
fgets() actually received the entire contents of the input string. When he
was having trouble figuring it out even after I recommended he look
carefully at the difference between the two cases when the input was short
enough and when the input was too long, I suggested he look at the FAQ for
this newsgroup. He came back and said he couldn't find anything, so I went
and looked and, although I thought I remembered seeing it there before, I
couldn't find anything either.

Could someone provide the topic number that deals with trapping this
occurance (if one exists)?

I found 12.20 ( http://www.eskimo.com/~scs/C-faq/q12.20.html ) right away
and this seems a natural spot to place a link to the topic that actually
goes into this issue at length.

Is there a version of the FAQ someplace that is searchable? Or, if the
eskimo site is searchable, could someone explain how? I haven't downloaded
the ASCII version and I'm assuming I can search it, but I can't find an
on-line version of it which, from a utility standpoint, is reasonably
important if I'm going to get students to use it since they move from
machine to machine a lot.

TIA
 
M

Malcolm

William L. Bahn said:
I recently challenged one of my students to figure out a way to
determine if fgets() actually received the entire contents of the input
string.
Could someone provide the topic number that deals with trapping
this occurance (if one exists)?
We had a huge row about this a few years ago and the FAQ maintainer still
hasn't come round to admitting that I was right.
 
W

William L. Bahn

Malcolm said:
We had a huge row about this a few years ago and the FAQ maintainer still
hasn't come round to admitting that I was right.

Right about WHAT?!?! That it is there? That it isn't there? That is should
be there? That it shouldn't be there? That it should be linked to the one I
mentioned? That it shouldn't be linked to the one I mentioned? You seem to
think that everyone is a mind reader and should immediately know what you
are thinking about when you respond with vague references. That you had a
huge row about "this" a few years ago tells me absolutely nothing of value.
It only gives the appearance that you are making vague claims so that it is
impossible for anyone to refute the claim that you were right about whatever
it was the alleged huge row a few years ago was about.
 
P

Peter Nilsson

William L. Bahn said:
I recently challenged one of my students to figure out a way to determine if
fgets() actually received the entire contents of the input string. When he
was having trouble figuring it out even after I recommended he look
carefully at the difference between the two cases when the input was short
enough and when the input was too long, I suggested he look at the FAQ for
this newsgroup. He came back and said he couldn't find anything, so I went
and looked and, although I thought I remembered seeing it there before, I
couldn't find anything either.

I would have thought it more appropriate to search the func specs for
fgets(). The FAQ isn't a tutorial.
Could someone provide the topic number that deals with trapping this
occurance (if one exists)?

I found 12.20 ( http://www.eskimo.com/~scs/C-faq/q12.20.html ) right away
and this seems a natural spot to place a link to the topic that actually
goes into this issue at length.

Is there a version of the FAQ someplace that is searchable?

I often just browse and search the last posting to clc via google...

http://groups.google.com/[email protected]
 
C

CBFalconer

Peter said:
I would have thought it more appropriate to search the func
specs for fgets(). The FAQ isn't a tutorial.

I agree. The OP should make a copy of N869.txt available for all
his students. This is almost the ultimate reference.

It is simplest to avoid the testing for conditions etc. by using
ggets. This has the simplicity of gets, without the
insecurities. Available at:

<http://cbfalconer.home.att.net/download/ggets.zip>
 
M

Malcolm

William L. Bahn said:
Right about WHAT?!?!

Here's the FAQ's replacement of gets() with fgets().

#include <stdio.h>
#include <string.h>

char answer[100], *p;
printf("Type something:\n");
fgets(answer, sizeof answer, stdin);
if((p = strchr(answer, '\n')) != NULL)
*p = '\0';
printf("You typed \"%s\"\n", answer);

Obviously this is bugged. Less obviously, it is actually worse than using
gets(), because wrong behaviour isn't necessarily superior to undefined
behaviour. (The argument about which is worse is a bit pointless, since the
solution is to use correct behaviour, but it led to a huge row and as a
result the FAQ has not been debugged.)
 
V

Villy Kruse

William L. Bahn said:
Right about WHAT?!?!

Here's the FAQ's replacement of gets() with fgets().

#include <stdio.h>
#include <string.h>

char answer[100], *p;
printf("Type something:\n");
fgets(answer, sizeof answer, stdin);
if((p = strchr(answer, '\n')) != NULL)
*p = '\0';
printf("You typed \"%s\"\n", answer);

Obviously this is bugged. Less obviously, it is actually worse than using
gets(), because wrong behaviour isn't necessarily superior to undefined
behaviour. (The argument about which is worse is a bit pointless, since the
solution is to use correct behaviour, but it led to a huge row and as a
result the FAQ has not been debugged.)


I think that each program must decide what should happen when reading a
partial line with fgets. In some circumstances it is quite appropriate
to silently truncate the line on input. In other situations it should be
treated as a serius error in the input data. As it stands an oversized
input line will silently be cut into two or more chunks each of which
looks like a separate line to the program. Depending on circumstances
that can be fatal or benign. A line overflow with gets will hardly
ever be a benign problem, and, what is worse, it isn't detected easily.


If the '\n' character is missing from the read line the line is either
truncated or you hit end of file before the newline at the end of the
last line.

Villy
 
D

Dave Thompson

I recently challenged one of my students to figure out a way to determine if
fgets() actually received the entire contents of the input string. <snip>

(you mean line)
<snip> I suggested he look at the FAQ [site] <snip>

Your substantive question has mostly been answered; the if in 7.1
hints at the issue but doesn't really explain it.
Is there a version of the FAQ someplace that is searchable? <snip>

The plaintext version is posted periodically -- monthly IIRC, although
worldnet has abysmal retention on comp.answers so it disappears -- and
available from http://www.eskimo.com/~scs/C-faq/versions.html
as well as the usual repositories for (all?) big-8 FAQs:
http://www.faqs.org/faqs/C-faq/ (lightly HTMLized)
ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top