Go Down

Topic: Read a file from SD in reverse order (Read 1 time) previous topic - next topic

ibilling

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.

PaulS

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.

johnwasser

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

ibilling

Thanks John - that will work perfectly!!

Go Up