4 ms·
In Unix, a text file is defined as a sequence of zero or more lines, each terminated by a newline. If the file has any lines at all, then the last line is term
by kazinator 6y ago
In Unix, a text file is defined as a sequence of zero or more lines, each terminated by a newline.
If the file has any lines at all, then the last line is terminated by a newline, for the same reason as the first and any other line.
If the last line is missing a newline, then it's an improper text file.
An empty file is just no characters at all. If a file contains nothing but a newline character, then it's a one-line file containing an empty line.
If the newline were optional at the end of a file, then those two cases (empty file versus one-line file with optional newline missing) would not be distinguishable.
The text file representation has to have some sort of unambiguous framing which indicates the presence of each line. Unix chose the terminating newline as that framing.
A lot of tools come from the Unix environment. C came from Unix, and the C <stdio.h> text streams follow Unix conventions when operating in text mode, regardless of platform. So that is to say, unless you fopen a file in binary ("b") mode, text mode is in effect, and text mode means newline-terminated lines. The programming model that you see when manipulating text streams in C is that of lines terminated by '\n', and nothing else. This is true even if you are on Windows, where the actual file has "\r\n" termination, and character 26 at the very end.
C ate the world, especially in the area of tooling, and so that representation and its associated concepts have spread. Other languages imitate the model.
A lot of languages have been written in C in the last 30
years, and it's easy for such languages to be influenced by C's conventions. For instance, I think that Python has essentially the same newline-terminated-line view of text files, on any platform.
Language designers want to give programmers a sane model for working with text files that is platform-independent, and the inspiration from that comes from UNIX via C more than anything due to historic circumstances being what they are.
Plus the text file model is very simple. It has no special case for the last line (no file terminator), and uses a single character for the framing, which is a lot simpler to program with than a two character sequence like CR-LF, where either of the two characters can be missing, or they can be in the wrong order: all these cases to handle in every situation where you're scanning across lines.