I'm trying to visualize newlines in an input file by making ^^M active and displaying it as \n. However, the \input command appears to append an extra empty line at the end of the file, which I don't want.
According to tex.pdf:
486. An empty line is appended at the end of a read_file.
Here's a minimal example demonstrating the issue:
\documentclass{article}
\begingroup
\catcode`\^^M=13%
\gdef\visiblenewline{%
\catcode`\^^M=13%
\def^^M{ \textbackslash n }%
}%
\endgroup
\begin{document}
\visiblenewline\input{file.txt}%
\end{document}
file.txt:
Line 1
Line 2
Line 3
Line 4
Line 5
(Note: file.txt has no trailing empty lines)
Expected Output
Line 1 \n Line 2 \n Line 3 \n Line 4 \n Line 5
Actual Output
Line 1 \n Line 2 \n Line 3 \n Line 4 \n Line 5 \n
The extra \n at the end is the problem. How can I prevent this automatically appended empty line from appearing, or strip it during input? I'm looking for a TeX-level workaround for this behavior.

\catcode`\^^M=13%better use=\activeor=13 %or the following commands will be expanded before the assignment