How to write to a text file sequentially in the same text line?

I would like to write a text file from a LaTeX document with the following format:

A,C,D,B

where A, C, D and B are to be written into the text file from different places of the LaTeX document. Thus, I cannot at once write

A,C,D,B

but

A,

and then

C,

and so on.

Paul

You can accumulate in a token register and write them all together

\documentclass{article}

\newtoks\mywritetoks
\newwrite\mywritefile
\def\mywrite#1{%
\global\mywritetoks{#1}%
\else
\global\mywritetoks\expandafter{\the\mywritetoks,#1}%
\fi
}

\begin{document}

\mywrite{A}

\mywrite{C}
\mywrite{D}
\mywrite{B}

\immediate\openout\mywritefile=\jobname.zzz
\immediate\write\mywritefile{\the\mywritetoks}
\immediate\closeout\mywritefile
\end{document}


Produces

A,C,D,B


in \jobname.zzz