# File input and output

Are there any LaTeX packages that can assist with reading and writing to files? I am trying to develop a solution to the following problem:

1) I am producing a long report regularly. Part of the report include items like

   Key date: 12 Sep 2010    Complete Floor 1
Key date: 18 Sep 2010    Other activity


2) It will be nice to have all these dates saved into a sort of an aux file and then import later as a section of its own. It will also need to be sorted.

I have so far looked at the answers package for inspiration and also thought of hacking the index mechanism. Any ideas and pointers would be greatly appreciated.

-
I wrote an answer about reading and writing files, but maybe if you describe more of what you're trying to accomplish, someone can provide a more targeted answer. –  TH. Aug 26 '10 at 20:45
Are you absolutely sure that you want to do this in Latex? What about writing a simple script (in Python, Perl, or whatever scripting language you prefer) that reads your report and generates Latex code? –  Jukka Suomela Aug 26 '10 at 20:54
Since the data is generated while someone is typing sections of the report, it makes sense to attempt to do this with \TeX. This way data can be entered via a macro, e.g. \kdate{12/09/2010}{Complete Floor 1}. The file operations would just summarize all these instances. –  Yiannis Lazarides Aug 26 '10 at 22:40

Sounds like the perfect task for the datatool package. It is design to create "databases" in TeX, which can then be saved to an external file and then read in later. Lots of useful data manipulation commands, as well.

-

You can easily write to a file using

\newwrite\file
\immediate\openout\file=myfilename.txt
\immediate\write\file{A line of text to write to the file}
\immediate\write\file{Another line of text to write to the file}
\closeout\file


You can read from a file a line at a time in a similar way.

\newread\file
\openin\file=myfilename.txt
\loop\unless\ifeof\file
% Do something with \fileline
\repeat
\closein\file


Edit: Note that \unless is an e-TeX extension. \readline is another useful extension. It reads the line and assigns each character a category code of other (12) or space (10). This is true even of the end of line character.

-
The second example contains a bug. \read\file to \fileline should be invoked before \unless\ifeof\file. I tested your answer several hours ago, now I have changed the order, see my answer. –  xport Aug 8 '11 at 23:27
loop and repeat are also extensions / macros: tex.ac.uk/ctan/info/latex2e-help-texinfo/latex2e.html –  Ciro Santilli Aug 19 at 8:13

If some of your report data is table like, perhaps the pgfplotstable package is an option. It makes it really easy to read in (parts) of delimited text files and typeset the data as nice tables.

-
This package seems to be a good proposition. –  Yiannis Lazarides Aug 26 '10 at 22:49

I tried all the answers given so far - except I did not spent too much time on the datatool mentioned by Will Robertson. At the end I decided to hack a simple solution which is shown below:

 %% Define a new command for activity key-dates
%% this can be saved for shipout later
\newcommand{\keydate}[2]{#1  #2 \\}
\newcommand{\out}[2]{\immediate\write\tempfile{\noexpand\keydate{ #1}{#2}}}

%% We open the file to  to write the key-dates
%% we will use it later to import
\newwrite\tempfile
\immediate\openout\tempfile=keydates.tex

%% Example
%%

\out{Activity 1}{10 Sep 2010}
\out{Activity 2}{12 Sep 2010}
\out{Activity 3}{13 Sep 2010}

%% finally we close the file
%%
\immediate\closeout\tempfile

%% we now read the file and dostuff with it
%%
\section{Summary of  Key Dates}
\input{keydates}


This quick hack did the job. On the next version, I will try using the datatool in order to be able to manipulate the data more extensively.

-