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$
5
  • 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, 2016 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$ May 10, 2017 at 5:36
  • 3
    $\begingroup$ I'm voting to close this question as un-reproducible. $\endgroup$
    – Jason B.
    Mar 7, 2019 at 18:45
  • 1
    $\begingroup$ This bug is fixed in V11.3 $\endgroup$
    – m_goldberg
    Mar 7, 2019 at 21:26
  • $\begingroup$ @JasonB. Did you try with Mathematica 10.0? $\endgroup$
    – a06e
    Mar 8, 2019 at 15:07

3 Answers 3

3
$\begingroup$

It appears that this is a bug.

$\endgroup$
2
$\begingroup$

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

enter image description here

$\endgroup$
3
  • $\begingroup$ I'm using version 10.0.2, what version are you on? $\endgroup$
    – M.R.
    Jan 30, 2015 at 1:21
  • $\begingroup$ See edit for my system version. $\endgroup$
    – a06e
    Jan 30, 2015 at 14:16
  • 1
    $\begingroup$ Yes in the latest version ReadLine has a few flaws. $\endgroup$
    – M.R.
    Jan 30, 2015 at 14:21
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];
$\endgroup$
2
  • $\begingroup$ Have you reported this to the tech support? $\endgroup$ May 10, 2017 at 4:17
  • $\begingroup$ I haven't Alexey as I tend to use OpenRead anyway, but yourself (or anybody) are quite welcome to :) $\endgroup$
    – foxcode64
    May 10, 2017 at 4:22

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.