Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #392

Re: fgets - design deficiency: no efficient way of finding last character read

From Jasen Betts <jasen@xnet.co.nz>
Newsgroups comp.lang.c.moderated
Subject Re: fgets - design deficiency: no efficient way of finding last character read
Date 2012-04-30 21:59 -0500
Organization Dis (not Dat) Organisation
Message-ID <clcm-20120430-0007@plethora.net> (permalink)
References <clcm-20120423-0011@plethora.net>

Show all headers | View raw


On 2012-04-23, John Reye <jononanon@googlemail.com> wrote:
> Hello,
>
> The last character read from fgets(buf, sizeof(buf), inputstream) is:
> '\n'
> OR
> any character x, when no '\n' was encountered in sizeof(buf)-1
> consecutive chars, or when x is the last char of the inputstream
>
> ***How can one EFFICIENTLY determine if the last character is '\n'??
> "Efficiently" means: don't use strlen!!!

intialise the last character in the buffer with \0 before calling fgets
if it gets changed to anything other than \n then an incomplete line
was read.

OTOH strlen is much faster than most I/O so does it really matter?

> A well-designed fgets function should return the length of characters
> read, should it not??
>
> Please surprise me, that there is a way of efficiently determining the
> number of characters read. ;)
>
> I've thought of ftell, but I think that does not work with stdin.

yeah, it works with files, stdin is usually not a file.

> Because right now, I think that fgets really seems useless.
> Why is the standard C library so inefficient?

what's the point of using fgets on stdin anywaqy

> Do I really have to go about designing my own library? ;)
>
> Thanks for tipps and pointers

libreadline  (GPL)
libgettext   (BSD)
getline      (posix.1 2008)

-- 
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

fgets - design deficiency: no efficient way of finding last character read John Reye <jononanon@googlemail.com> - 2012-04-23 08:33 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read Barry Schwarz <schwarzb@dqel.com> - 2012-04-30 21:57 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read Jasen Betts <jasen@xnet.co.nz> - 2012-04-30 21:59 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read James Kuyper <jameskuyper@verizon.net> - 2012-04-30 21:57 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read Dag-Erling Smørgrav <des@des.no> - 2012-04-30 21:57 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read Thomas Richter <thor@math.tu-berlin.de> - 2012-04-30 21:58 -0500
  Re: fgets - design deficiency: no efficient way of finding last character read John Reye <jononanon@googlemail.com> - 2012-04-30 21:59 -0500

csiph-web