The answers package appears to do something like this (do texdoc answers, or the equivalent on your platform, for the documentation).
If that doesn't work, or isn't really suitable, then I've got a hand-rolled solution to exactly this problem which I could post here.
(duplicated from a stackoverflow question)
OK: Sam Nead, in the comments, wanted to see the hand-rolled version, so I've included it in part below. The comments report an inheritance from Victor Eijkhout's comment.sty, but he's not to blame for the way I've butchered it. This isn't complete, as it doesn't show the output file being read in at the end. It illustrates that if you want to write out stuff with backslashes, you might have to do entertaining things with catcodes. Certainly, I learned a few things from examining and adapting Eijkhout's code.
No warranty expressed or implied; contents may settle during handling; do not ingest; and keep well out of the reach of children.
%%% {examples} environment -- problem/example, to be collected at the end
%
% \if@examples is a flag to tell us if we've seen any examples yet.
\newif\if@examples \@examplesfalse
\newwrite\@exampleaux
\@definecounter{example}
%
% Don't use \newenvironment. Instead use the fact that \begin{env}
% turns into \begingroup\env . This code is simplified from
% Eijkhout's comment.sty
%
% The \@bsphack...\@esphack pair doesn't seem to want to work, here
% (well, it does in horizontal, but not in vertical, mode).
%
% The {example} environment takes an optional argument, giving the
% number of dangerousbend symbols to attach to the example. The
% default is zero (negative numbers are treated the same as zero,
% numbers larger than about 2 are probably silly, but will work).
%
% Unfortunately, because of the \@ifnextchar, and the fact that the
% contents of this environment are processed oddly, you can't have
% anything expandable immediately after the \begin{example}, unless
% you write \begin{example}[0] or \begin{example}\relax.
%
% The end of the environment must be \end{example} alone on a line
% (ie, no whitespace before or after). The same is true for the
% beginning and end of the {examplenotes} environment.
%
\def\makeother#1{\catcode`#1=12 }
\def\example{\@bsphack\@ifnextchar[%]
\@example{\@example[0]}}
% \@openexamples opens the examples file. Nilpotent.
\def\@openexamples{%
\if@examples \else
\immediate\openout\@exampleaux\jobname.examples%
\immediate\write\@exampleaux{\relax}%
\global\@examplestrue
\fi
}
\def\theexample{\@arabic\c@example}
\def\@example[#1]{%
\@openexamples
\refstepcounter{example}%
% Read in contents line by line, with all catcodes `other'
\let\do\makeother \dospecials %
\makeother\^^L%
\endlinechar`\^^M \catcode`\^^M=12 %
\immediate\write\@exampleaux %
{\string\begin{examplebody}%
{\theexample}%
{\csname the\LN@currsectionlevel\endcsname}%
{#1}%
}%
\if@screenpdf %
\marginpar{\small{\hyperlink{ex-\@arabic\c@example}{Ex.\theexample}}}%
\else %
\marginpar{\small{See example \theexample}}%
\fi %
\@tempswatrue
\ProcessExampleLine %
}
%
% Define \EndExampleTest, with all catcodes other
{\escapechar=-1 \xdef\EndExampleTest{\string\\end\string\{example\string\}}}
{\escapechar=-1 \xdef\BeginNotesTest{\string\\begin\string\{examplenotes\string\}}}
{\escapechar=-1 \xdef\EndNotesTest{\string\\end\string\{examplenotes\string\}}}
% Write out the body of the {example}, carefully surrounding the
% content of the {examplenotes} environment with
% \ifexamplenotes...\fi. Note this _requires_ that the beginning and
% end of the {examplenotes} environment appear on lines by
% themselves. Change the escape character so that we can write out
% \if..\fi without problems.
{\catcode`\^^M=12 \endlinechar=-1 \catcode`\|=0 |catcode`|\=12 %
|gdef|ProcessExampleLine#1^^M{|def|test{#1}%
|if@tempswa
|immediate|write|@exampleaux{\ifexampletext}|@tempswafalse
|fi
|ifx|BeginNotesTest|test %
|immediate|write|@exampleaux{\fi\ifexamplenotes}%
|fi %
|ifx|EndExampleTest|test %
|edef|next{%
|immediate|write|@exampleaux{\fi\end{examplebody}}%
|@esphack %
|endgroup %
}%
|else %
|immediate|write|@exampleaux{#1}%
|ifx|EndNotesTest|test %
|immediate|write|@exampleaux{\fi\ifexampletext}%
|fi %
|let|next|ProcessExampleLine %
|fi %
|next}%
}
% Add text to the examples file
\long\def\addtoexamples#1{%
\@openexamples
{\@temptokena{#1}%
\immediate\write\@exampleaux{\the\@temptokena}}}