Re: removing newline character from the buffer read by fgets
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 09:35:46 -0600
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
.
- References:
- removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- Re: removing newline character from the buffer read by fgets
- From: Walter Roberson
- removing newline character from the buffer read by fgets
- Prev by Date: Re: Most Interesting Bug Track Down
- Next by Date: Re: scanf behaviour
- Previous by thread: Re: removing newline character from the buffer read by fgets
- Next by thread: Re: removing newline character from the buffer read by fgets
- Index(es):
Relevant Pages
|