Re: removing newline character from the buffer read by fgets



junky_fellow@xxxxxxxxxxx wrote:
On Nov 28, 3:27 pm, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
junky_fel...@xxxxxxxxxxx said:

On Nov 28, 1:59 pm, Richard Heathfield wrote:
The principal objection I have to your code is that it fails to provide a
way to distinguish between a line that has been read completely and a
line that has been read only partially because of insufficient space in
the buffer.

For that I would change the function to return the number of bytes
read.That is a reasonable approach. I would suggest a size_t type for this. You
might reasonably reserve 0 for an exceptional condition, leaving the user
to enquire of ferror() to find out whether Something Really Bad happened or
whether the routine simply couldn't find any data to stuff into the buffer
(i.e. encountered EOF on the first getc call).


Regarding "size_t", I used the type "int" for "bytes_read" as its value
will never be greater than "n". The type of "n" is "int", so I thought
"bytes_read" should also be "int".

Well consider pure DOS which is a 16 bit system. An int would have a
range of -32768 to 32767. On the other hand you might be able to
allocate an object of 65536 bytes. You might want to read in the
contents of a file into this buffer. If your function used int as the
type to specify bytes to read, the caller will have to call your
function twice. Had you used size_t instead, you could be assured that
you can fully address the largest single object you could possibly
allocate.

size_t is expressly meant for the purpose of holding the size of
objects in your program, so it might be more suited for this purpose
than an int, (atleast a signed int).

.



Relevant Pages

  • [RFC: 2.6 patch] remove the broken SCSI_ACORNSCSI_3 driver
    ... * Abandoned using the Select and Transfer command since there were ... Once debugged, remove the #undef, otherwise to debug, ... -unsigned int dmac_address ... * Purpose: differentiate between commands that have a DATA IN phase ...
    (Linux-Kernel)
  • [PATCH] Numerous fixes to kernel-doc info in source files.
    ... static inline int ffs ... @buffer: where the data must be copied. ... * struct kfifo with kfree. ... @timer: ...
    (Linux-Kernel)
  • [UNIX] Multiple Vulnerabilities in Citadel/UX
    ... could allow complete control over a vulnerable server. ... Citadel server as can be seen by this simplistic code snippet: ... configuration buffers, leading to the possibility of carrying out a buffer ... int connect_to_host; ...
    (Securiteam)
  • Re: pushing the envelope with sockets
    ... receiving on the socket they are received (upto the buffer size), you can even change what happens if the buffer runs full. ... int read = S.EndReceive; ... class AsyncReader: Reader ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Updated: Reworked Cell OProfile: SPU mutex lock fix
    ... arch-independent pieces of the OProfile kernel driver that this patch ... kernel driver patches. ... into the kernel buffer without holding the buffer_mutex lock. ... int spu_sync_start; ...
    (Linux-Kernel)