Re: removing newline character from the buffer read by fgets



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 the
buffer read by
fgets() ?Others here have posted sensible suggestions on the best way to remove the
newline character from the buffer after an fgets call. But if you're really
after efficiency, don't use fgets!

Is there any library function that is similar to fgets() but also tells
how many
bytes it read into the buffer ?No such standard library function exists, but you could write a routine
yourself to do it. It could also have the following characteristics:

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>

.



Relevant Pages

  • 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)
  • Re: *scanf in Harbison and Steele
    ... The difference between fgets and gets is at least that fgets takes ... the output buffer. ... Though I've been told that stdin come with the food in C, must you declare: ...
    (comp.lang.c)
  • Re: user input, getchar, and buffer - For C beginners and those with teaching skills...
    ... buffer: video streaming, printer jobs. ... > Fgets unlike scanf has a limit in the number of caracters ... to read any newline left (here it would be located in array> "chaine" ... than 20 characters and keeps it in the buffer? ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... OK, for the sake of discussion, replacing fgets() with some type ... and you just want to do some simple parsing and searching. ... the START of the line in the buffer, ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... OK, for the sake of discussion, replacing fgets() with some type ... of equivalent function where you START with a unopened file really ... reset the buffer position. ... if you only want to set buffer position, ...
    (comp.lang.c)