This question follows this Applying TikZ options stored in a file. I just wonder why the following fails to read the file. Please ignore the content of filecontents; although it works, it's just for developing a MNWE. The problem is with my use of \input.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{myaux.tex}
\documentclass{article}
\makeatletter
\def\chapno{1}
\def\exno{1.1}
\chardef\other=12
\newwrite\ans
\par\medbreak
\immediate\write\ans{}%
\immediate\write\ans{\string\ansno\chapno.\exno:}%
\copytoblankline
}
\def\copytoblankline{\begingroup\setupcopy\copyans}
\def\setupcopy{%
\def\do##1{\catcode##1=\other}%
\dospecials
\catcode\|=\other
\obeylines%
}
\def\ifnextisspace{%
\expandafter\futurelet\expandafter\next
\expandafter\ifn@xtisspace\string
}
\def\ifn@xtisspace{%
\ifx\next\@sptoken\expandafter\gobblespace
\else\expandafter\writetofile\fi
}
\@namedef{gobblespace} {\ifnextisspace}
\def\writetofile#1\@nil{\immediate\write\ans{#1}}

{\obeylines %
\gdef\copyans#1
{\def\next{#1}%
\ifx\next\@empty %
\let\next=\endgroup %
\else %
\expandafter\ifnextisspace\next\@nil %
\let\next=\copyans %
\fi %
\next %
}%
}

yyy \ifx xx
\fi zzz

\begin{document}
x
\end{document}
\end{filecontents}

\makeatletter
\begingroup
\lccode\A=1 \lccode\B=1 \catcode\A=7
\lowercase{\endgroup\def\catch@marker{AB}}
\edef\reserved@a{%
\long\def\noexpand\catchfile##1\catch@marker{%
\noexpand\unexpanded\noexpand\expandafter{##1}%
}%
}
\reserved@a
\def\catchfilecontent#1#2{%
\begingroup
\everyeof\expandafter{\catch@marker\noexpand}%
\makeatletter
\edef\reserved@a{\endgroup
\long\def\noexpand#1{%
\expandafter\catchfile\expandafter\@empty
\@@input #2
}%
}%
\reserved@a
}
\makeatother

% Test
\catchfilecontent\myauxfile{myaux.tex}

\begin{document}
x
\end{document}

• Trying your example, I don't get any \input-related error, but a complaint about Illegal parameter number in definition of \myauxfile which is exactly the same as if I copy the contents of filecontents directly into \long\def\myauxfile{. So the problem does seem to lie inside filecontents. – Stephan Lehmke Jun 11 '12 at 4:04
• If I replace # by ## inside filecontents, it works. As for the direct definition. So the file catching seems to work as expected (what else did you expect from Heiko? :) – Stephan Lehmke Jun 11 '12 at 4:33
• The problem is exactly that TeX finds #1 when scanning the replacement text for \def\myauxfile{<contents of the file>}. The catchfile package takes precautions against this, so \CatchFileDef\myauxfile{myaux.tex}{} works perfectly. – egreg Jun 11 '12 at 7:23
• @StephanLehmke: Thanks. I don't how Heiko came in here. I was torture-testing a scheme I saw by David Carlisle, on the expandability of TeX's original \input. I will fix the 'Illegal parameter' problem. – Ahmed Musa Jun 11 '12 at 16:18
• Sorry for misattribution. The code looked very similar to Heiko's package, so I thought you had modified that. But it's also possible it went the other way round ;-) – Stephan Lehmke Jun 11 '12 at 16:23

I haven't looked at the implementation of catchfile package, but verbatimizing # solves the problem. I can't yet think of another way out.

\def\catchfilecontent#1#2{%
\begingroup
\def\do##1{\catcode##1=12}%
\dospecials
\catcode\|=12
\obeylines%
\everyeof\expandafter{\catch@marker\noexpand}%
\makeatletter%
\edef\reserved@a{\endgroup%
\long\def\noexpand#1{%
\expandafter\catchfile\expandafter\@empty%
\@@input #2 %
}%
}%
\reserved@a%
}

• Heiko Oberdiek uses a \toks register to store the file's contents, so that the # are doubled and the subsequent \edef works. – egreg Jun 11 '12 at 17:03
• @egreg: That's why I tried \unexpanded, eTeX's expandable way of using \toks, but apparently it didn't work without verbatimizing special characters. In fact, I have also tried the \toks` based approach. – Ahmed Musa Jun 11 '12 at 18:57