Re: removing newline character from the buffer read by fgets



On Tue, 28 Nov 2006 04:04:06 +0000 (UTC), roberson@xxxxxxxxxxxxxxxxxx
(Walter Roberson) wrote in comp.lang.c:

In article <1164682990.902780.19980@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
junky_fellow@xxxxxxxxxxx <junky_fellow@xxxxxxxxxxx> wrote:
Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?

Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?

fgets() terminates the string with the null character, so you can
simply use something along the lines of

/* code fragment follows, not intended as standalone routine */

fgetsresult = fgets( outbuffer, sizeof(outbuffer)-1, stream);
if (fgetsresult == NULL) {
/* handling for no string available */
} else {
size_t newbuflen = strlen(outbuffer);
if (outbuffer[newbuflen - 1] == '\n') outbuffer[newbuflen - 1] = '\0';
}

In addition to Martin's comments, which I agree with, this is overly
complicated. See my simpler method in my reply to the original.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Null-terminated strings: the final analysis.
    ... If you use fgets, you can see any '\0' that you had previously written, ... prefilling the buffer and calling fgets, you can scan the buffer backwards ... Whether a text file requires a new-line character on the last line is ...
    (comp.lang.c)
  • Re: sorting the input
    ... of data in the "line" leaves at least two unused bytes in the buffer, ... buffer to mean the "array" pointed to by the first argument to fgets. ... contents of the stream buffer. ... new-line character or after end-of-file. ...
    (comp.lang.c)
  • Re: Null-terminated strings: the final analysis.
    ... If you use fgets, you can see any '\0' that you had previously written, ... prefilling the buffer and calling fgets, you can scan the buffer backwards ... It is clear that NUL in a text stream corrupts the string. ... Handling the last line without '\n' is trivial: If fgets returns non-null, the first '\0' in the buffer will have been placed there by fgets after the last character read from the stream. ...
    (comp.lang.c)
  • Re: Using MBCS in a UNICODE defined project
    ... if(fgets(buffer, sizeof(buffer)/sizeof, f)) ... because buffer is always char ... Just wanted to show "the right way" of calculating character count. ... The MSDN meaning is "what the programmer understands by character" (code unit ...
    (microsoft.public.vc.mfc)
  • system() and _flushall()
    ... You must explicitly flush (using fflush or _flushall) or close any stream ... However, on the next read from the input file using fgets, I get ... forgotten that there's still data in the buffer (the buffer is not destroyed ...
    (microsoft.public.vc.language)