Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So I have a .txt-file and I need to import the data line per line. That works like it should. I need however to adapt the contents loaded into a command an write it to another .txt file that later will be used by the datatool package. Problem is that if I write this:

\documentclass{article}

\begin{document}

\newopen\copyfile  % open the file to write to
\openout\copyfile=copy.txt

\newread\file      % open the file to read from
\openin\file=file.txt

\loop\unless\ifeof\file
  \read\file to\fileline  % Read one line and store it into \fileline

  \fileline                   % print the content into the pdf
  \write\copyfile{\fileline}  % print the content to copy.txt
\repeat

\end{document}

The output file looks like this:

\par
\par
\par
...

While the pdf displays the actual content of the file. Here is an example of a text file for input.

data.txt:

This is the first line.
And the second one.
Here is the third.
And so on...

If anyone has another solution of making datatool skip the first x lines, or even better when reading a certain string, that will solve my problem too in a more efficient way :)

p.s.: I cannot adapt the .txt-files

share|improve this question
    
I tried \expand and \noexpand btw –  Didii Jul 16 '12 at 10:09
add comment

1 Answer

up vote 6 down vote accepted

You're forgetting \immediate:

\documentclass{article}

\begin{document}

\newwrite\copyfile  % open the file to write to
\immediate\openout\copyfile=copy.txt

\newread\file      % open the file to read from
\openin\file=data.txt

\begingroup\endlinechar=-1
\loop\unless\ifeof\file
  \read\file to\fileline  % Read one line and store it into \fileline
  \fileline\par                   % print the content into the pdf
  \immediate\write\copyfile{\unexpanded\expandafter{\fileline}}  % print the content to copy.txt
\repeat
\endgroup

\end{document}

I've added also a trick for avoiding a final blank in the expansion of \fileline. However the copy won't be exact, because TeX always add an empty line at the end of read in files.

The \unexpanded\expandafter{\fileline} avoids problems with expansion of commands possibly present in the data file.

It's quite difficult to understand why you'd want this.

share|improve this answer
    
Thanks for the quick answer! Will test it immediately. I need this because I need to import data using the package datatool. I have a large number of files which need to be processed. The text file starts with a bunch of info, and all I need to import in datatool is the table beneath all that info. Thus, my workaround: create a new .txt without the info above the table to make datatool import it correctly. –  Didii Jul 16 '12 at 11:17
    
Works flawlessly :) Thanks again! –  Didii Jul 16 '12 at 11:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.