| Deutsch English Français Italiano |
|
<8734gmpotb.fsf@bsb.me.uk> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse <ben@bsb.me.uk> Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Mon, 10 Feb 2025 01:32:16 +0000 Organization: A noiseless patient Spider Lines: 26 Message-ID: <8734gmpotb.fsf@bsb.me.uk> References: <vo9g74$fu8u$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 10 Feb 2025 02:32:16 +0100 (CET) Injection-Info: dont-email.me; posting-host="e74fee584a8beda3c5e232027f2f12e3"; logging-data="959044"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cQCjo4YJ5sQnY1GhWYNR7PeNDZggXiRk=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:RYijcG0d+X4N251Q/gfTOgmqXMo= sha1:GKe92hKVpVcVrEfLCcpbBnfFgec= X-BSB-Auth: 1.08a816b6d56c694d3c22.20250210013216GMT.8734gmpotb.fsf@bsb.me.uk Bytes: 1882 Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > To get the last line of a text file I'm using > > char buf[BUFSIZ]; > while (fgets (buf, BUFSIZ, fd) != NULL) > ; // read to last line > > If the end of the file is reached my test shows that the previous > contents of 'buf' are still existing (not cleared or overwritten). Something that has not yet come up (as far as I can see) is that you might need to handle an empty file. In such a case, nothing gets written and fgets returns NULL right away. Processing buf in this situation is then undefined. One way to handle this is to put into buf something that can't get read by fgets. Two newlines is a good candidate: char buf[BUFSIZE] = "\n\n"; You can then test for that if need be, though of course it all depends on what your application is doing. -- Ben.