# ReadLine[file] after reopening a closed stream doesn't read at the beginning of file?

Take a text file from your system:

file = OpenRead["path/to/your/file"]

Close[file]


Here ReadLine[file] should return the first line from the file. Now reopen the closed stream and read a line again:

file = OpenRead["path/to/your/file"]



I'd expect to get the first line again. However, in my system this returns the second line of the file. That is, closing a file and reopening it doesn't reset the read position.

Is this supposed to happen? From my usage of other languages such as C++ I'd expect that closing and reopening a file should reset the reading position to the beginning of the file.

Edit: I am using Mathematica 10.0.2, Ubuntu 14.04.1.

Edit 30/1/20159: This is clearly unwanted behavior. I have added the tag bug. I have not been able to contact Wolfram support because their webpage freezes for me for some reason, so please some else make the bug report.

• in addition to the noted bug ReadLine is inexplicably throwing a "cannot be used with binary data" error on files that are absolutely plain text. If they ever pick up the phone on the issue let then know about that too. – george2079 Sep 13 '16 at 16:05
• This seems OS-specific. I don't reproduce this with versions 10.4.1 and 11.1.1 on Windows 7 x64. – Alexey Popkov May 10 '17 at 5:36
• I'm voting to close this question as un-reproducible. – Jason B. Mar 7 '19 at 18:45
• This bug is fixed in V11.3 – m_goldberg Mar 7 '19 at 21:26
• @JasonB. Did you try with Mathematica 10.0? – becko Mar 8 '19 at 15:07

Use Read[file, String] instead of ReadLine[file].

• I'm using version 10.0.2, what version are you on? – M.R. Jan 30 '15 at 1:21
• See edit for my system version. – becko Jan 30 '15 at 14:16
• Yes in the latest version ReadLine has a few flaws. – M.R. Jan 30 '15 at 14:21

It appears that this is a bug.

I find that an error occurs when you try to get ReadLine (giving it the filename) to open a file subsequent times. For example here with Linux MMA 11.1.1:

ReadLine["/tmp/numbers.txt"]


Out:

1   2   3   4   5   6


So far so good. Now I close the file:

Close["/tmp/numbers.txt"]


Out:

/tmp/numbers.txt


Now I try to reopen the file:

ReadLine["/tmp/numbers.txt"]


Out:

Read::openx: InputStream[/tmp/numbers.txt,3] is not open.


Don't know if that happens on any other operating system. Seem to get around it by manually opening the file first:

file = OpenRead["/tmp/numbers.txt"];