# How to append data to a temporary file?

I want to create a new temporary file (named main-temp.tex), append data to it multiple times, and import the contents later. I haven't learnt how to use TeX file operation, both reading and writing.

In this question, I think I need writing operation only because reading data from a file line by line is not needed in my scenario below. Thus using \input{} is enough to read all data.

% main.tex

\documentclass{article}
\usepackage{xcolor}

{
% code to append #1 to a shared temporary file named main-temp.tex
}

{%
% code to markup  question #1
% for example
{\color{blue}#1}
%
% code to append answer #2
}

\begin{document}
\section{Questions}
\AddQuestion{Are you kidding?}{No. I am not a joker.}

% imports answers from the shared temporary file named main-temp.tex
\input{main-temp}

\end{document}


How to append data to a temporary file? I know using --enable-write18 switch is mandatory when invoking latex. I don't want to use the existing package such as exercise, etc.

-
You can't append to an existing file using TeX, except by reading it in first and writing it out again with the new material attached. I think we had a question for this already. Searching ... –  Martin Scharrer Jul 17 '11 at 9:03
Duplicate of How can I open a file in “append” mode?. Please confirm. –  Martin Scharrer Jul 17 '11 at 9:04
On second thought: Do you need to share this file across different documents? Or do you just want to accumulate material in an external file and read it in again like e.g. ToC? Then however it would look like a duplicate of What is the basic mechanism for writing something to an aux file?. [I had to undelete this question to add this comment.] –  Martin Scharrer Jul 17 '11 at 9:23
Sure, no problem. If you need more help, then just update this question instead of deleting it. –  Martin Scharrer Jul 17 '11 at 9:25
TeX can't append to an already existing file. It can open a new file (or overwrite an existing file) and write multiple times to it. Your title and text suggested to me that this file is shared between multiple documents or one document and an external application. –  Martin Scharrer Jul 17 '11 at 10:32

The basic code is as follows.

\documentclass{article}
\newwrite\ans
\immediate\openout\ans=\jobname-temp

\begin{document}
\section{Questions}

\AddQuestion{Are you kidding?}{No. I'm not a joker.}

\end{document}


You are responsible for adding formatting directives to the answers file. You might profit from the \unexpanded feature, to avoid expanding commands during the \write operation:

\newcommand{\AppendAnswer}[1]{\immediate\write\ans{\unexpanded{#1}^^J}}


is a good way, but probably you'll need a mix of expanded and unexpanded writes: the reference number, for example, would need to be expanded. So, say you have a counter that keeps track of the questions

\newcounter{question}[section]
\renewcommand{\thequestion}{\thesection.\arabic{question}}


after having defined \AddQuestion to also increment the question counter.
The final ^^J means that each answer will be followed by an empty line.