Re: removing newline character from the buffer read by fgets



On 27 Nov 2006 19:03:10 -0800, "junky_fellow@xxxxxxxxxxx"
<junky_fellow@xxxxxxxxxxx> wrote in comp.lang.c:

Is there any efficcient way of removing the newline character from the
buffer read by
fgets() ?

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

/* inside a function */
if (NULL != fgets(my_buffer, sizeof my_buffer, stdin)
{
char *nlptr = strchr(my_buffer, '\n');
if (nlptr) *nlptr = '\0';
}

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

You can certainly write one, a simple wrapper for fgets():

size_t my_fgets(char *buffer, size_t size, FILE *fp)
{
size_t result = 0;
if (NULL != fgets(my_buffer, sizeof my_buffer, fp)
{
/* optional, remove newline */
char *nlptr = strchr(my_buffer, '\n');
if (nlptr) *nlptr = '\0';
result = strlen(my_buffer);
}
return result;
}

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