lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


gary ng <garyng2000@yahoo.com> writes:
> char *fgets(char *buf, int n, FILE *)
>
> the return is a null terminated string, not the length
> actually read. So any null in the input stream if not
> treated specially would terminate the string and the
> rest being lost(as there is no way to distinguish
> whether that null is appended by fgets() or is in the
> input).

I don't think the rest of the string is actually lost though -- if
you're careful, you can differentiate:  basically search for a newline
within the buffer (the first n bytes), ignoring '\0'; if you find one,
then it should be followed by '\0', and that's a real string terminator;
any other '\0' is part of the input string.  [If you hit the end of the
buffer without finding a newline, then you've got a truncated line.]

[I suspect real fgets implementations simply don't care about '\0' at
all, and treat it like any other non-newline character.]

-Miles

-- 
The key to happiness
 is having dreams.      [from a fortune cookie]