Re: Null-terminated strings: the final analysis.
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Mon, 13 Apr 2009 07:13:07 -0400
Keith Thompson wrote:
Flash Gordon <smap@xxxxxxxxxxxxxxxxx> writes:It is clear that NUL in a text stream corrupts the string. Period.Harald van Dijk wrote:On Sun, 12 Apr 2009 22:57:30 +0100, Flash Gordon wrote:My excuse for the long and convoluted method is to show I was handlingHarald van Dijk wrote:I stand corrected. It may even be simpler than you suggested: ignoring theOn Sun, 12 Apr 2009 13:32:39 -0700, Keith Thompson wrote:I think you can almost all the time, but it takes a little work...On the systems I use, if I write a '\a' character (ASCII BEL) to aIf you use fgets, you can see any '\0' that you had previously written,
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()).
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,
[snip pseudo-code]
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.
each possible case ;-)
I agree that filling will '\0' then back searching for the first
non-'\0' is better. Error still has to be handled seperately as it
leaves the buffer indeterminate. I think end-of-file after a byte has
been read is OK, but it is not explicitly stated as being OK.
Whether a text file requires a new-line character on the last line is
implementation-defined. If it's not required, and if the last line
happens to end with '\0', it's going to be very difficult to tell just
what happened after calling fgets().
Handling the last line without '\n' is trivial: If fgets returns non-null, the first '\0' in the buffer will have been placed there by fgets after the last character read from the stream. Find it with..
char *s;
s = strchr(buffer, '\0');
Check for '\n' with..
if (*(s-1) == '\n')
--
Joe Wright
"Memory is the second thing to go. I forget what the first is."
.
- References:
- Re: Null-terminated strings: the final analysis.
- From: Tony
- Re: Null-terminated strings: the final analysis.
- From: Mark McIntyre
- Re: Null-terminated strings: the final analysis.
- From: Mark Wooding
- Re: Null-terminated strings: the final analysis.
- From: CBFalconer
- Re: Null-terminated strings: the final analysis.
- From: Joe Wright
- Re: Null-terminated strings: the final analysis.
- From: Mark Wooding
- Re: Null-terminated strings: the final analysis.
- From: Mark McIntyre
- Re: Null-terminated strings: the final analysis.
- From: Keith Thompson
- Re: Null-terminated strings: the final analysis.
- From: Harald van Dijk
- Re: Null-terminated strings: the final analysis.
- From: Flash Gordon
- Re: Null-terminated strings: the final analysis.
- From: Harald van Dijk
- Re: Null-terminated strings: the final analysis.
- From: Flash Gordon
- Re: Null-terminated strings: the final analysis.
- From: Keith Thompson
- Re: Null-terminated strings: the final analysis.
- Prev by Date: Re: Reading data by words from a file in Linux system
- Next by Date: Re: for loop problem
- Previous by thread: Re: Null-terminated strings: the final analysis.
- Next by thread: Re: Null-terminated strings: the final analysis.
- Index(es):
Relevant Pages
|