Re: removing newline character from the buffer read by fgets
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 28 Nov 2006 02:08:15 -0800
junky_fellow@xxxxxxxxxxx wrote:
On Nov 28, 11:07 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
junky_fel...@xxxxxxxxxxx said:
Is there any efficcient way of removing the newline character from thenewline character from the buffer after an fgets call. But if you're really
buffer read by
fgets() ?Others here have posted sensible suggestions on the best way to remove the
after efficiency, don't use fgets!
Is there any library function that is similar to fgets() but also tellsyourself to do it. It could also have the following characteristics:
how many
bytes it read into the buffer ?No such standard library function exists, but you could write a routine
1) always reads a complete line (subject to available memory), by expanding
the buffer as and when necessary
2) allows you to re-use the same buffer over and over (like fgets)
3) records and reports the number of bytes read into the buffer per call
4) records and reports the current size of the buffer (*not* the same thing
as the length of the string contained therein)
5) doesn't bother putting a newline on the end, because there's no need,
because of Property 1, and therefore the caller need not remove it
Such a routine is far, far, far simpler to write than you might imagine.
thanks everyone for your help. In fact, I don't want to use strlen() or
strchr()
(for efficiency reasons) to remove the newline character.
Based of Richard's suggestion, I tried to write my own version of
fgets()
that will not put newline character to the input buffer passed.
I am sure, the people here will find lots of problems in my code. All
comments
are welcome.
following is my code:
/* it returns NULL , if EOF or error encountered else returns the
string pointer */
#include <stdio.h>
char *
my_fgets(char *string, int n, FILE *fp)
{
int ch;
int count = 0;
while(count < (n - 1)) {
Why (n - 1)?
<snip>
.
- 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: Richard Heathfield
- Re: removing newline character from the buffer read by fgets
- From: junky_fellow@xxxxxxxxxxx
- removing newline character from the buffer read by fgets
- Prev by Date: Re: Urgently required
- Next by Date: Re: Question regarding fgets and new lines
- 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
|