4
$\begingroup$

Take a text file from your system:

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

ReadLine[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"]

ReadLine[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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – george2079 Sep 13 '16 at 16:05
  • $\begingroup$ This seems OS-specific. I don't reproduce this with versions 10.4.1 and 11.1.1 on Windows 7 x64. $\endgroup$ – Alexey Popkov May 10 '17 at 5:36
  • 3
    $\begingroup$ I'm voting to close this question as un-reproducible. $\endgroup$ – Jason B. Mar 7 '19 at 18:45
  • 1
    $\begingroup$ This bug is fixed in V11.3 $\endgroup$ – m_goldberg Mar 7 '19 at 21:26
  • $\begingroup$ @JasonB. Did you try with Mathematica 10.0? $\endgroup$ – becko Mar 8 '19 at 15:07
2
$\begingroup$

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

enter image description here

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

It appears that this is a bug.

| improve this answer | |
$\endgroup$
1
$\begingroup$

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"];
ReadLine[file]
Close[file];
| improve this answer | |
$\endgroup$
  • $\begingroup$ Have you reported this to the tech support? $\endgroup$ – Alexey Popkov May 10 '17 at 4:17
  • $\begingroup$ I haven't Alexey as I tend to use OpenRead anyway, but yourself (or anybody) are quite welcome to :) $\endgroup$ – pfactors May 10 '17 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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