Pages: [1]   Go Down
Author Topic: Read a file from SD in reverse order  (Read 1335 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wish to display the last 20 entries from a .txt file on the SD card (Ethernet shield on a Mega2560).

Reading the whole file into an array an then iterating through the array backwards works until the file gets to a size that makes the array too large for RAM.

Any suggestions on a way of doing this that won't take up RAM?

If I write the array to flash (PROGMEM) will I be able to delete it and re write it (the array) again every time a new line is written to the SD file and, if so, will the flash memory be freed up after deletion?

All advice is appreciated.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I write the array to flash (PROGMEM) will I be able to delete it and re write it (the array) again every time a new line is written to the SD file and, if so, will the flash memory be freed up after deletion?
Flash memory is read only, so forget about writing to it.

Quote
Reading the whole file into an array an then iterating through the array backwards works until the file gets to a size that makes the array too large for RAM.
An array of 20 pointers, to point to (copies of) the last twenty lines, will always take up the same amount of space, regardless of how many lines there are in the file.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use file.position() to get the (unsigned long) file position before writing a new line.

Use file.seek(position) to set the file pointer back to the beginning of the known line.

As PaulS recommended: Keep a circular buffer of the last 20 file positions so you can read the last 20 lines in any order.

Note: if you want to read a line that is NOT at the end of the file and then add more lines to the end, be sure to note the end-of-file position before you read and seek that position before you write more.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John - that will work perfectly!!
Logged

Pages: [1]   Go Up
Jump to: