Douglas said:
Martin said:
| A text stream is an ordered sequence of characters composed into
| lines, each line consisting of zero or more characters plus a
| terminating new-line character. Whether the last line requires a
| terminating new-line character is implementation-defined. [...]
Let's assume an implementation that makes the choice (and documents it)
that the last line requires a terminating new-line character. Is such
an implementation required to also define and document the behavior of a
program which writes to a text stream without terminating the last line
with a new-line character?
No, only whether the last line requires a terminating new-line
character needs to be documented.
Otherwise, can it be assumed that the behavior of such a program is
undefined on such an implementation, since lack of explicit definition
of behavior in the standard means undefined behavior (4#2)?
Yes, and more importantly, if the program behavior depends on
whether or not the last line of an input text stream has a
trailing new-line, it is not a strictly conforming program.
(This is a common bug.)
Well, even if the requirement for trailing new-line is documented by
the implementation, who exactly is the requirement binding upon? The
programmer or the implementation? For example:
FILE *t = fopen("text.txt", "w");
fprintf(t, "Hello world");
fclose(t);
I suppose I have written eleven characters to text.txt and nothing
else. And so it would not be a line because the required '\n' is not
there. And perhaps not even a stream for the same reason. Again, who
is this requirement incumbent upon?
I have a Windows98 system. I open this file with a text editor,
change the w to W and save it.
EDIT.COM doesn't complain about missing '\n' but adds it saving.
notepad.exe doesn't complain and doesn't add it.
vim.exe declares [Incomplete last line] and adds '\n' saving.
wordpad.exe doesn't complain and doesn't add it.
winword.exe doesn't complain about missing '\n' but adds it saving.
As a C programmer of applications and utilities, I write text files
in lines ending in '\n'. Even the last one. I do not place this
restriction on people sending files to me. I will see the last line
as a line whether it has a '\n' or not.
Sorry to be so wordy. I suppose that the requirement for a
terminating '\n' or not, documented or not is not actually imposed
on anyone. I suppose it is a caveat only so that I (programmer) can
take care to the case when it isn't there. I suppose also that there
is no case for the last line not ending in '\n' is a requirement.