How to catch this exception?

  • Thread starter Lord Gas, the Waxen
  • Start date
B

Blah

Lefteris Kororos said:
Dan said:
char buf[100];
int x;

while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

^^^^^^^^^^^^^
Where does the -1 come from?

Dan
You need to account for the NULL terminator of the string.

I don't have any documentation handy, but IIRC fgets() handles that
detail for you, making the -1 above useful only if you want two bytes
available at the end of your buffer.
 
M

Martien Verbruggen

Dan said:
char buf[100];
int x;

while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

^^^^^^^^^^^^^
Where does the -1 come from?

Dan
You need to account for the NULL terminator of the string.

From the C99 standard:

2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character read into
the array.

Martien
 
T

Tom St Denis

Martien said:
Dan said:
char buf[100];
int x;

while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

^^^^^^^^^^^^^
Where does the -1 come from?

Dan

You need to account for the NULL terminator of the string.


From the C99 standard:

2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character read into
the array.

I always -1 because some functions [recv(), strncpy if I recall] don't
do this.

Instead of consulting my C specs I just always -1. If I really need
that 1 byte on a machine running 80 processes with 500MB of ram... then
I ought to revise my algorithms/program...

Tom
 
D

Dan Pop

Yes. I found that. I just wasn't absolutely sure that the function
wouldn't "perform" anything *else* in the event of a matching failure.

The standard clearly says that a matching failure causes the function to
return, right?

Dan
 
D

Dan Pop

In said:
Martien said:
Dan Pop wrote:




char buf[100];
int x;

while (fgets(buf, sizeof(buf)-1, stdin) != NULL) {

^^^^^^^^^^^^^
Where does the -1 come from?

Dan

You need to account for the NULL terminator of the string.


From the C99 standard:

2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character read into
the array.

I always -1 because some functions [recv(), strncpy if I recall] don't
do this.

Instead of consulting my C specs I just always -1. If I really need
that 1 byte on a machine running 80 processes with 500MB of ram... then
I ought to revise my algorithms/program...

That's what I suspected: it comes from your ignorance.

Dan
 
A

Arthur J. O'Dwyer

For the record, and because I know this is not obivous at all to
non-French speakers, and also because I care somewhat about it, my last
name is indeed Mollinier Toublet (with a space, not an hyphen), and I
have no middle name.

Oops. Sorry, Bertrand. I was looking at your name in the "From"
field of my mail reader (Pine)'s main screen, which displays things like

2868 Jul 1 Dan Pop (2476) . | | |-Re: How to catch th
2869 Jul 1 To: comp.lang.c (2846) . | | | |-Re: How to catch
2870 Jul 1 Bertrand Mollinier (2387) | | | |-Re: How to catc
2871 Jul 2 Dan Pop (3004) | | | |-Re: How to catc

(notice how all the fields get truncated). So I thought without
double-checking that Mollinier *was* the whole name! Whoops!
:)

-Arthur
 
P

Peter Shaggy Haywood

Groovy hepcat Lefteris Kororos was jivin' on Tue, 01 Jul 2003 23:02:18
+0100 in comp.lang.c.
Re: How to catch this exception?'s a cool scene! Dig it!
You need to account for the NULL terminator of the string.

Wrong! First of all, it's a null (or some people prefer NUL)
terminator, not NULL. NULL is a macro that expands to a null pointer
constant. And secondly, and more to the point, you do *not* have to
take that into account, since fgets() already does so.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
A

A. Sinan Unur

(e-mail address removed) (Peter "Shaggy" Haywood) wrote in
Groovy hepcat A. Sinan Unur was jivin' on 1 Jul 2003 03:56:47 GMT in
comp.lang.c.
Re: How to catch this exception?'s a cool scene! Dig it!
Tom St Denis said:
Lord Gas, the Waxen wrote:
... but, i mean, %d means decimal, so i have to put a number
... my program crushes if i put something different from a number.
[Snip.]
It pushes the data back on the stream, which is of course invalid
again. Common for MSVC.

Incidentally, this had nothing whatsoever to do with MSVC. The OP's
problem is addressed in the FAQ list:

http://www.eskimo.com/~scs/C-faq/q12.19.html

as well as:

http://www.eskimo.com/~scs/C-faq/q12.20.html

Both the OP and Tom St Denis would be well advised to peruse this
resource.

I'm sure Tom is well acquainted with the FAQ. His solution, which
you seem to have snipped, is suggested in question 12.20, whose URL
you have given above.

Yeah, I realized that after I posted. However, I found him labeling this
behavior a problem with MSVC is what threw me off. FWIW, apologies to Tom
for the snide remark.

Sinan.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top