Groups | Search | Server Info | Keyboard shortcuts | Login | Register


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

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

From John Reye <jononanon@googlemail.com>
Newsgroups comp.lang.c.moderated
Subject fgets - design deficiency: no efficient way of finding last character read
Date 2012-04-23 08:33 -0500
Organization http://groups.google.com
Message-ID <clcm-20120423-0011@plethora.net> (permalink)

Show all headers | View raw


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!!!

I only come up with the strlen method, which - to me - says that fgets
has a bad design.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  char buf[6];
  FILE *fp = stdin;
  while (fgets(buf, sizeof(buf), fp)) {
    printf((buf[strlen(buf)-1] == '\n') ? "Got a line which ends with
newline: %s" : "no newline: %s", buf);
  }


  return EXIT_SUCCESS;
}



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.

Because right now, I think that fgets really seems useless.
Why is the standard C library so inefficient?
Do I really have to go about designing my own library? ;)

Thanks for tipps and pointers

Regards,
J.

(PS: I've posted to comp.lang.c but don't see my post appearing, so
I'll try here instead)
-- 
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 | NextNext 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