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

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"];  
Read[rtmp];  
Write[atmp, "ABC"];  
ReadList[rtmp]  
{"b", "cde"}  

What happend to ABC?

Read[rtmp]  
EndOfFile  
FilePrint["tmp"]  
"a"
"b"
"cde"
"ABC"

OK.
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"];  
StreamPosition[atmp];  
Close[atmp];  
rtmp = OpenRead["tmp"];  
Read[rtmp];  
ReadList[rtmp];  
StreamPosition[rtmp];  
SetStreamPosition[rtmp, 1];  
Read[rtmp];  
FilePrint["tmp"];  
Close[rtmp];  
DeleteFile["tmp"];  
Streams[];
share|improve this question
    
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.) –  Mr.Wizard Dec 28 '12 at 13:30
    
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. –  Leonid Shifrin Dec 28 '12 at 14:24
2  
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. –  Mr.Wizard Dec 28 '12 at 21:26
    
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. –  Joel Klein Dec 28 '12 at 21:37
    
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. –  Joel Klein Dec 28 '12 at 21:44
show 1 more comment

1 Answer

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.

share|improve this answer
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.