# Failing to write to file and input the same

\documentclass{minimal}
\usepackage{etoolbox}

\newwrite\tempfile

\makeatletter
\let\features\@gobble
\makeatother
\newcommand{\feature}[1]{%
\expandafter\def\expandafter\features\expandafter{\features,#1}%
}


\expandafter\ifdefempty\expandafter{\csname features#1\endcsname}
{\expandafter\appto\csname features#1\endcsname{#2}}
{\expandafter\appto\csname features#1\endcsname{,#2}}%
}

\def\lstText#1{\csname features#1\endcsname}

\makeatletter
\newcommand{\outText}[1]{%
\immediate\openout\tempfile=\jobname_#1.tex
\immediate\write\tempfile{\lstText{#1}}
\immediate\closeout\tempfile
}
\makeatother

\makeatletter
\let\inputIFE\@input
\makeatother

\begin{document}

\inputIFE{test_A}

\newText{A}

Birds and \textbf{bears}
}
\lstText{A}
\outText{A}

\end{document}

• Please never use the minimal class, it is not meant for examples, Mar 26 '20 at 18:15
• Perhaps you should start by explaining exactly what your end goal is here? It is not clear to me what you are trying to do. Mar 26 '20 at 18:18

Compiling your example gives an undefined control sequence: \@nil error. \write carries an \edef-like expansion. The error comes from the full expansion of \textbf. So some care should be paid to the expansion.

Suppose you want to write one-step expansion result of command \features<#1> to file \jobname_#1.tex in \outText, try this:

\newcommand{\outText}[1]{%
\immediate\openout\tempfile=\jobname_#1.tex
% write out content of \feature<#1>
\immediate\write\tempfile{%
\expandafter\expandafter\expandafter\unexpanded
\expandafter\expandafter\expandafter{\csname features#1\endcsname}}%
\immediate\closeout\tempfile
}