Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15861

Re: Read last expression in a file?

From Mark McClure <mcmcclur@unca.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Read last expression in a file?
Date 2011-02-03 10:34 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iie0bl$bsp$1@smc.vnet.net> (permalink)

Show all headers | View raw


On Sat, Jan 29, 2011 at 7:43 PM, Ramiro <ramiro.barrantes@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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Read last expression in a file? Mark McClure <mcmcclur@unca.edu> - 2011-02-03 10:34 +0000

csiph-web