# newwrite: Adding text to a existing outfile

I have

\documentclass[a4paper]{article}
\begin{document}
\newwrite\myoutfile
\immediate\openout\myoutfile=\jobname-myoutfile.txt
\immediate\write\myoutfile{
aaa
}
\immediate\closeout\myoutfile

\section{Test: Input the Result}
\input{\jobname-myoutfile.txt}
\end{document}


which gives me the external file jobname-myoutfile.txt with

aaa


inside.

How can I add new text, like bbb (in a newline!), to the outfile?

So that I will have

aaa
bbb

• why are you closing the file if you want to append more? – Ulrike Fischer Apr 18 '20 at 12:04
• @UlrikeFischer Because I do not know all the "newwrite"-commands. – cis Apr 18 '20 at 12:05
• @UlrikeFischer Ah Ok, I think I understand – cis Apr 18 '20 at 12:07
• @cis As soon as you close a file you cannot append to it (with just TeX facilities); you can't input it unless you first close it. – egreg Apr 18 '20 at 12:07

It should work like

\documentclass[a4paper]{article}

\begin{document}
\newwrite\myoutfile
\immediate\openout\myoutfile=\jobname-myoutfile.txt
\immediate\write\myoutfile{
aaa
}

\immediate\write\myoutfile{
bbb
}

\immediate\closeout\myoutfile

\section{Test: Input the Result}
\input{\jobname-myoutfile.txt}
\end{document}