Groups | Search | Server Info | Login | Register
Groups > comp.lang.c.moderated > #395
| From | Dag-Erling Smørgrav <des@des.no> |
|---|---|
| Newsgroups | comp.lang.c.moderated |
| Subject | Re: fgets - design deficiency: no efficient way of finding last character read |
| Date | 2012-04-30 21:57 -0500 |
| Organization | Usenet Fact Police |
| Message-ID | <clcm-20120430-0002@plethora.net> (permalink) |
| References | <clcm-20120423-0011@plethora.net> |
John Reye <jononanon@googlemail.com> writes: > 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!!! You can't. This is one of several reasons not to use fgets(). If you're on a POSIXish platform (which includes most Unix derivatives), you can use getline() instead: ssize_t getline(char **buf, size_t *size, FILE *f); where - buf is a pointer to a char * which is either NULL or points to a malloc()ed buffer; getline() will malloc() or realloc() as needed. - size is a pointer to a size_t which contains the size of the buffer, i.e. the size argument from the last malloc() or realloc() call. - f is the stream to read from. I wish they had placed it first in the argument list instead of last. - the return value is the length of the string (i.e. what strlen() would return), or -1 if an error occurred or EOF was reached before any data was read. See also http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/gets DES -- Dag-Erling Smørgrav - des@des.no -- 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 | Next — Previous in thread | Next in thread | Find similar
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