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


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

Re: Read last expression in a file?

Started byMark McClure <mcmcclur@unca.edu>
First post2011-02-03 10:34 +0000
Last post2011-02-03 10:34 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

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

#15861 — Re: Read last expression in a file?

FromMark McClure <mcmcclur@unca.edu>
Date2011-02-03 10:34 +0000
SubjectRe: Read last expression in a file?
Message-ID<iie0bl$bsp$1@smc.vnet.net>
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

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web