# How to read a counter register from a file in Plain TeX?

I am a TeX user, and I would like to read a number from an auxiliary file in order to achieve things like: printing at the footing "page number X out of Total". I was trying to use the primitives commands \openin and \read, but \read only reads text into macros (according to the TeXbook page 217) and I don't see how I could read a number from a file into a count register.

Read the file as written in the previous run.

\input plipsum % for mock text

% save the total page number in a file (hook into \bye)
\outer\def\bye{\par\vfill\supereject\writetotal\end}
\def\writetotal{%
\immediate\openout\totalfile=\jobname.tot
\immediate\write\totalfile{\the\pageno}%
\immediate\closeout\totalfile
}
\newwrite\totalfile

% at the start we read the file
% the file doesn't exist
\def\totalpage{0}%
\else
% define \totalpage to the contents of the first line in the tot file
% it will have a space at the end
\fi

\footline{\hfil Page number \folio\ out of total \totalpage\unskip\hfil}

\lipsum{1-40}

\bye


If you insist to have a count register, just set it:

\input plipsum

\outer\def\bye{\par\vfill\supereject\writetotal\end}
\def\writetotal{%
\immediate\openout\totalfile=\jobname.tot
\immediate\write\totalfile{\the\pageno}%
\immediate\closeout\totalfile
}
\newwrite\totalfile
\newcount\totpages

\def\totalpage{0}%
\else