Re: removing newline character from the buffer read by fgets



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';
}
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
.



Relevant Pages

  • 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: 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)
  • 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: sorting the input
    ... line shorter than the buffer. ... reading a line means. ... If the amount of data processed by fgets fills your buffer, ... will be the _th character fgets read followed by a '\0'. ...
    (comp.lang.c)