The package zref, using the lastpage module is an option. The information about the page count is written to the *.aux file.
Since a class file is to be modified, the "Programmer's" interface of zref, \zref@extract{<reference name>}{<property name>} should be used to retrieve the information.
The *.aux file is read on subsequent compilation runs just before starting to typeset the document, making the information stored in the *.aux file available.
Custom auxiliary output files need to be opened first before they can be written to.
\documentclass{article}
\usepackage{kantlipsum}
\usepackage[lastpage]{zref}
\newwrite\mytocfile
\immediate\openout\mytocfile=mytocfile.txt
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%overwrite the `\maketitle' definition of the class file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\let\maketitleOrig\maketitle
\def\maketitle{%
\immediate\write\mytocfile{The last page number is: \zref@extract{LastPage}{page}.}%
\immediate\write\mytocfile{The total page count is: \zref@extract{LastPage}{abspage}.}%
\maketitleOrig%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
%\makeatletter
%
%%this my go into your cls file
%\newwrite\mytocfile
%\immediate\openout\mytocfile=mytocfile.txt
%\AtBeginDocument{%
% \immediate\write\mytocfile{The last page number is: \zref@extract{LastPage}{page}.}%
% \immediate\write\mytocfile{The total page count is: \zref@extract{LastPage}{abspage}.}%
%}
%
%\makeatother
\title{The Document Title}
\begin{document}
\maketitle
{\huge
The last page number is \makeatletter\zref@extract{LastPage}{page}.\makeatother
The absolute page count is \makeatletter\zref@extract{LastPage}{abspage}.\makeatother
}
\pagenumbering{arabic}
\kant[1-10]
\pagenumbering{Roman}
\kant[11-20]
\end{document}
\AtEndDocument{\immediate\write\mywrite{\thepage}}– touhami Jan 29 '16 at 12:00\zref@extract{LastPage}{abspage}where needed within your class file. – AlexG Jan 29 '16 at 12:40and got a "Missing number, treated as zero" error message
– m42 Jan 29 '16 at 13:05LastPageinformation into some external file. That has been done for you already. Simply insert\zref@extract{LastPage}{abspage}at the place within your*.clswhere you need this information. – AlexG Jan 29 '16 at 13:15*.clsfile to write theLastPagevalue into an external file. I.e., part of functionality of the*.clsfile is to create an external file with some content (used for other purposes). – m42 Jan 29 '16 at 13:18\maketitleand this doesn't work well with\AtBeginDocument. – m42 Jan 29 '16 at 13:42\maketitlecommand of an existing class file? – AlexG Jan 29 '16 at 13:49\makefilehappens after\begin{document}. Therefore, you don't need\AtBeginDocumentif you modify\maketitle. See my edit. – AlexG Jan 29 '16 at 14:00AtBeginDocument. In my case I need to use\protected@writeinstead of\immediate\writedue to the use of several other variables I need to expand. – m42 Jan 29 '16 at 14:10