Ho provato ma non andava. Poi ho aggiunto \newwrie\ans e va. Però non mi stampa le risposte.
`\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{exercise}{Esercizio}[section]
\makeatletter
\newwrite\ans
\def\answer{\par
\immediate\write\ans{}
\immediate\write\ans{\string\item[\theexercise]}
\copytoblankline}
\def\copytoblankline{\begingroup\setupcopy\copyans}
\def\setupcopy{\ex@sanitize\obeylines}
{\obeylines \gdef\copyans#1
{\def\next{#1}%
\ifx\next\empty\let\next=\endgroup %
\else\immediate\write\ans{\next} \let\next=\copyans\fi\next}}
\def\ex@sanitize{\def\do##1{\catcode`##1=12 }\dospecials
\@tempcnta=128
\loop\ifnum\@tempcnta<256
\catcode\@tempcnta=12
\advance\@tempcnta\@ne
\repeat}
\def\inputanswers{\immediate\closeout\ans
\section*{Risposte}
\begin{enumerate}
\input{\jobname.ans}
\end{enumerate}}
\makeatother
\begin{document}
\section{Titolo}
testo
\begin{exercise}
Quanto fa $2+2$?\answer $4$
\end{exercise}
\inputanswers
\end{document} `
Mi dice che manca il file nomefile.ans.