Sunday, 15 August 2010

Reading a file backwards in Mathematica -- How? -



Reading a file backwards in Mathematica -- How? -

i have big file (written in mathematica) contains n "records" , each of these records list of fixed length m, n > 10,000 , 500 < m < 600 (bytes). note, scheme not have capacity hold records in memory --- reason writing them file. have application (in mathematica) needs process these records in reverse order; i.e. lastly record written out first record processed. how can read these records file in reverse order?

meanwhile (after trial , error mathematica i/o) found 1 solution. note, stripped downwards illustration of possible solution.

fname = "testfile"; strm = openwrite[fname]; n = 10; (* in general, n big *) for[k = 1, k <= n, k++, (* create list on each pass through loop ... *) pot = {{k, k + 1}, {k + 2, k + 3}}; print[pot]; (* save file *) write[strm, pot]; ]; close[strm]; (* 2nd pass byte offsets of each record written file *) strm = openread[fname]; byteindx = {0}; for[i = 1, <= n, i++, pin = read[strm]; appendto[byteindx, streamposition[strm]]; ]; drop[byteindx, -1] (* read records in reverse order *) for[i = n, >= 1, i--, setstreamposition[strm, byteindx[[i]]]; pin = read[strm]; print[pin]; (* process pin ... *) ]; close[strm];

it nice if 2nd pass (to byte offsets) eliminated have not found how yet... also, these byte offsets written file (similar how records handled) , read in 1 @ time, should there still memory problem.

for sake of putting answer, sec pass can written concisely:

strm = openread[fname]; byteindx=reap[while[sow[streamposition[strm]]; !trueq[read[strm ] == endoffile]]][[2,1,;;-2]] n=length[byteindx]

wolfram-mathematica

No comments:

Post a Comment