NYCPHP Meetup

NYPHP.org

[nycphp-talk] fgets(): why bytes - 1

David Mintz dmintz at davidmintz.org
Wed Mar 2 17:20:43 EST 2005


A little theoretical question to which I *know* you guys know the
answer so don't be coy (-:

The fgets() function documentation says:

string fgets ( resource handle [, int length] )

Returns a string of up to length - 1 bytes read from the file pointed to
by handle. Reading ends when length - 1 bytes have been read, [...]

How come it's length - 1? My research suggests the answer is, because it
PHP'wraps  fgets() a C function of the same name which does it that way.

OK then, why does the C function fgets() do it that way? I heard a rumor
that says the C function appends a NULL character to the string, so that
might explain it -- to leave some room. But if so, why the null character?

Gratefully,


---
David Mintz
http://davidmintz.org/



More information about the talk mailing list