Re: Null-terminated strings: the final analysis.



On Sun, 12 Apr 2009 22:57:30 +0100, Flash Gordon wrote:
Harald van Dijk wrote:
On Sun, 12 Apr 2009 13:32:39 -0700, Keith Thompson wrote:
On the systems I use, if I write a '\a' character (ASCII BEL) to a
text file, I can reasonably expect to see a '\a' character when I read
it back. The same is not true of '\0' if I use fgets() to read it
(though I think can see the '\0' if I use fgetc()).

If you use fgets, you can see any '\0' that you had previously written,
but you've got to be careful to make sure you don't treat it as a
terminator. You cannot reliably determine whether the '\0' is a
terminator,

I think you can almost all the time, but it takes a little work...
[snip pseudo-code]

I stand corrected. It may even be simpler than you suggested: ignoring the
possibilities of EOF and errors (which you've already handled), after
prefilling the buffer and calling fgets, you can scan the buffer backwards
to find the last '\0' byte. Everything before, including any other '\0'
bytes, were read from the file.
.



Relevant Pages

  • Re: Null-terminated strings: the final analysis.
    ... I can reasonably expect to see a '\a' character when I read ... If you use fgets, you can see any '\0' that you had previously written, ... terminator. ... prefilling the buffer and calling fgets, you can scan the buffer backwards ...
    (comp.lang.c)
  • Re: Null-terminated strings: the final analysis.
    ... I can reasonably expect to see a '\a' character ... If you use fgets, you can see any '\0' that you had previously ... treat it as a terminator. ... return cnt; ...
    (comp.lang.c)
  • Re: text length with an terminator
    ... The total length is suppose to be 113 (Including terminator), ... fgetl return a length of 112 and fgets return a length of 114. ... Kind regards, Jan ...
    (comp.soft-sys.matlab)
  • Re: Null-terminated strings: the final analysis.
    ... I can reasonably expect to see a '\a' character when I read it ... If you use fgets, you can see any '\0' that you had previously written, ... but you've got to be careful to make sure you don't treat it as a ... terminator, but you can reliably detect many instances where it is not: ...
    (comp.lang.c)
  • Re: text length with an terminator
    ... The total length is suppose to be 113 (Including terminator), ... fgetl return a length of 112 and fgets return a length of 114. ... They will probably be 10 13 corresponding to LineFeed, CarriageReturn ...
    (comp.soft-sys.matlab)