One OutputStream and one InputStream are opened on the same file:

atmp = OpenAppend["tmp"];  
Write[atmp, "a"];  
Write[atmp, "b"];  
Write[atmp, "cde"];  
rtmp = OpenRead["tmp"];  
Write[atmp, "ABC"];  
{"b", "cde"}  

What happend to ABC?


One after the other, close one, open the other.
But still skeptical.
Shouldn't I be able to set the StreamPosition now?

atmp = OpenAppend["tmp"];  
Write[atmp, "a"];  
Write[atmp, "bc"];  
Write[atmp, "def"];  
rtmp = OpenRead["tmp"];  
SetStreamPosition[rtmp, 1];  
  • $\begingroup$ I'm surprised I had not noticed this before. Good question. (Please do try to format your questions in an easily readable way as I did for you above.) $\endgroup$
    – Mr.Wizard
    Dec 28, 2012 at 13:30
  • $\begingroup$ My understanding is that it is best to not read and write to the file at the same time. The cursor of a read stream has all the rights to not follow the changes introduced by the write (output) stream. Or, it may block the actual writing until it is closed. In any case, this is an error-prone practice which I would simply avoid. Close one stream, and then work with the other one, and vice versa. $\endgroup$ Dec 28, 2012 at 14:24
  • 2
    $\begingroup$ Please: (1) Do not post extensions to the question as answers; edit the question instead. (2) Use the formatting tools to format your code, etc. $\endgroup$
    – Mr.Wizard
    Dec 28, 2012 at 21:26
  • $\begingroup$ You might want SetStreamPositionp[rtmp, 0] to rewind to the beginning. As it is, you're setting the stream after the opening quotation mark and asking Read to read a" (a lower-case a character followed by a quotation mark) as a Mathematica expression, hence the Read::readt message. $\endgroup$
    – Joel Klein
    Dec 28, 2012 at 21:37
  • $\begingroup$ It's hard to answer this without a clear question. The original questions seems to have been about reading from a stream that is also being written, and I agree with Leonid that I would not expect that to work. The followup edit seems to be "I should be able to SetStreamPosition but I get a message when I evaluate it", but it's hard to tell for sure because all the functions have their output suppressed. $\endgroup$
    – Joel Klein
    Dec 28, 2012 at 21:44

1 Answer 1


What happend to ABC?

Input Streams "read ahead" and buffer data from their sources.

When you write rtmp = OpenRead["tmp"], OpenRead reads the whole file "tmp" into memory. This is called buffering. (It reads up to 16 kilobytes, but your file's not that large.) All subsequent operations on the rtmp stream are really just operating on pre-loaded data in that file-in-memory buffer. Read reads data from the buffer, SetStreamPosition indicates a relative position in that buffer. If the file is bigger than 16K and you eventually need more data from it, another 8K will be "read ahead" as a single chunk into the buffer.

What you seem to want is a constantly up-to-date view of the actively-changing contents of the file on disk, which Mathematica streams will not provide for you. (This is because you won't get that from FILE* streams in C, and C is the language used to write the low-level parts of Mathematica.)

I can't understand what you are asking in the second part of your question. Don't just give a series of commands -- show their actual output, and explain what you expect that output to be.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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