Re: Read last expression in a file?
- To: mathgroup at smc.vnet.net
- Subject: [mg116163] Re: Read last expression in a file?
- From: Mark McClure <mcmcclur at unca.edu>
- Date: Thu, 3 Feb 2011 05:34:48 -0500 (EST)
On Sat, Jan 29, 2011 at 7:43 PM, Ramiro <ramiro.barrantes at gmail.com> wrote:
> I wrote a program that generates a lot of Mathematica output.
> Now I need to restart it using the very last expression of such output
> as the new input. However, I can't find a way to only read the last
> expression in a file, something like a Read that would start at the
> end.
This can be done using Mathematica's low level file manipulation
commands, such as Read and SetStringPosition. Here's an example.
First, we create a file to play with.
data == RandomInteger[{0, 1}, {100000, 100}];
Export["temp.txt", data];
If you examine the contents of temp.txt, you will find that it is a
30MB file containing one valid Mathematica list per line. The
following method depends on the file containing one valid expression
per line.
strm == OpenRead["temp.txt"];
p == SetStreamPosition[strm, Infinity];
While[
Read[strm, Character] ==!== "\n",
p == SetStreamPosition[strm, p - 1]];
lastLine == Read[strm]
Close[strm]
Mark McClure