# Can TeX detect a newline at the end of the file?

Is TeX able to distinguish beween a file containing, say, the single character a/ byte 97; and a file containing this same character followed by a newline marker (which is system-dependent)?

I had a bit of trouble creating a file with no trailing newline, so in case you need it, here is one way:

luatex "\directlua{io.output('test1.txt'):write('a') io.output('test2.txt'):write('a\string\n')}\bye"


Can TeX distinguish test1.txt from test2.txt (besides their name)? It seems that \read and \readline both discard a trailing newline and set \ifeof to true at the same time regardless of the presence of a trailing newline.

-

## 1 Answer

TeX also removes space characters at the right side of the input line, then it inserts the character configured via \endlinechar. The character is not inserted if \endlinechar is negative or greater than 255. The last restriction is different in LuaTeX/XeTeX. Because of the removal/replacing at the end of line vanilla TeX is not able to distinguish these files. Therefore TeX cannot read arbitrary binary files, e.g. to extract the size of an image file, …

In pdfTeX provides \pdffiledump. It returns the data, specified via offset and length, as hex dump. Therefore the end of file can be analyzed for end of line markers. (Package bmpsize uses \pdffiledump to analyze image file formats for meta data like width and height.)

And LuaTeX provides Lua functions for file I/O.

-
Thank you Heiko. It seems like a recurring theme in my questions to find out that XeTeX lacks the appropriate pdfTeX primitive and lacks the Lua functions provided by LuaTeX :(. –  Bruno Le Floch Aug 4 '12 at 11:53