Re: trailing blank line in a text file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 20 Jul 2004, Robert Locke wrote:

> I'm not sure about the php code being referred to, but a more generic
> problem for my understanding....
>
> How does one have a line without an end to the line?  I always assumed
> that all lines ended with <LF>, including the last line of the file....
> I can conjecture the creation of text at the end of the file without a
> <LF> at their end, but that to me is not really a line.  I guess I fall
> back to more of a read loop, I suppose....
>
> Of course, this is the beauty of Unix/Linux: we can each define as we
> see fit.....
>
> --Rob

In Unix, there are no line-oriented files.  Lines are a human construct
for imposing order on what would otherwise appear to be chaos.

Unix has record-oriented files (not an issue here) and stream-oriented
files.  Stream-oriented files appear to the filesystem as a single,
unbroken string of characters, some of which may be '^J'
(newline/linefeed).  Many (most) programming languages have routines that
will read the stream until the next '^J', so that programmers can think
"naturally" in terms of lines, but this should not be construed as making
the files line oriented.

So a stream file can end with any character.  In order to make these
stream files fit with a line-oriented world view, many (most) text editors
enforce that all lines--including the last one--end with '^J'.  Some
editors will allow this restriction to be overridden, and some programs
will break on inputs created by those editors.

When a program reads stream files, Unix will return characters until
either it is told to stop or it attempts to read beyond the end of the
file.  A program that calls read routines that return the string of
characters ending at the next newline will behave differently if the last
line does not end with a newline.  If the last newline is present, the
program will read the last line without getting an end-of-file indication,
and will only fail on the next read.  If the last newline is missing, the
program will get the end-of-file indication when it reads the last line.
This can cause "fencepost" errors such as the one the OP had in his PHP
snippet.

-- 
		Matthew Saltzman

Clemson University Math Sciences
mjs AT clemson DOT edu
http://www.math.clemson.edu/~mjs



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux