NYCPHP Meetup

NYPHP.org

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

John Nunez john at cyber-ny.com
Wed Mar 2 17:35:38 EST 2005


Hey David,

C uses Null Terminated strings. This means that a string can contain 
any character except for NULL. Like other non-printable control 
character such as BELL, TAB, etc...

Check out this link for a better explanation : 
http://www-ccs.ucsd.edu/c/charset.html#null-terminated%20string

- John

On Mar 2, 2005, at 5:20 PM, David Mintz wrote:

>
> 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/
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>




More information about the talk mailing list