The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Reading a file in reverse order --- How?
Replies: 3   Last Post: Apr 11, 2013 4:11 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Virgil Stokes

Posts: 77
Registered: 3/16/06
Reading a file in reverse order --- How?
Posted: Feb 14, 2013 2:08 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Here is a very simple example of saving lists (writing to a file) and then
reading the lists back in the same order as they were written. The problem that
I would like to get a solution to is how can the lists be *read in reverse
order?* Thus, the first list saved would be the last list to be inputted.

Note, in general, the length of each list is the same and rather small; but, the
number of lists written to the file can be rather large (n > 10,000), at least
too large to keep all of them in memory.

fname = "testfile";

strm = OpenWrite[fname];
n = 3; (* In general, n could be very large *)
For[k = 1, k <= n, k++,
(* Create list on each pass through this loop ... *)

POt = {{k, k + 1}, {k + 2, k + 3}};
(* Print[POt]; *)
(* Save list to a file *)
Write[strm, POt];

(* Other processing performed... *)

(* Open file with lists *)
strm = OpenRead[fname];
(* Process, sequentially, each list that was saved *)
For[i = 1, i <= n, i++,
PIn = Read[strm];
(* Print[PIn]; *)

(* Process PIn ... *)


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.