13

I don't know too much about writing and reading to and from files. But I managed to create a nice command for me where I need to write something into a text-file and later-on read it again.

\newcommand{\customwrite}[1]{
  \newwrite\tempfile%
  \immediate\openout\tempfile=filename_#1.txt%
  \immediate\write\tempfile{\the\figheight}%
  \immediate\closeout\tempfile%
}

The reading is achieved by

\newcommand{\customread}[1]{
  \newread\tempfile%
  \openin\tempfile=filename_#1.txt%
  \read\tempfile to \fileline%
  \setlength\figheightex\fileline%
  \closein\tempfile%
}

Both things works fine for me as long as I have not too much of this calls. Recently, I had a bigger document and everything failed and I got the message No room for a new \write. I searched for this error and what I found is, Latex is only capable of opening 16 write and 16 read streams. Apparently \closeout and \closein don't actually close these streams what I thought.

My question is now: is it somehow possible to actually close these streams?

8
  • you can use \tempfile repeatedly, without more than one \newwrite. Or am I missing something? And you shloud have a seperate one \mytempout for writing and \mytempin for reading...
    – yo'
    Oct 9, 2013 at 12:10
  • Ok, sorry. I truncated too much from my file. filename.txt actually changes by an optional argument, I corrected this in my posting. Furthermore, I can use \tempfile repeatedly. I only need this "variable" once while executing the command. and since I always only execute one of the commands, it was working for me using the same "variable" \tempfile for writing and reading
    – bene
    Oct 9, 2013 at 12:14
  • @tohecz you should make that an answer. Oct 9, 2013 at 12:27
  • @bene as tohez says just move the \new.... out of the definitions and just execute them once in the preamble. Oct 9, 2013 at 12:28
  • If you are just saving the height of a figure, why use a file at all rather than just save the macro? Oct 9, 2013 at 12:30

1 Answer 1

16

You reserve a new read/write slot every time you read or write, but you don't need it. Moving \new... out of the definitions does the trick:

\newwrite\mytempout
\newcommand{\customwrite}[1]{%
  \immediate\openout\mytempout=filename_#1.txt %
  \immediate\write\mytempout{\the\figheight}%
  \immediate\closeout\mytempout
}

\newread\mytempin
\newcommand{\customread}[1]{%
  \openin\mytempin=filename_#1.txt %
  \read\mytempin to \fileline
  \setlength\figheightex\fileline
  \closein\mytempin
}

I added and removed some of the %s to make it more reasonable, see e.g. Where are the necessary places to be appended with % to remove unwanted spaces?

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .