how to add a lines of data to the top of the column?

If doesn't fit into any other category ....
Post Reply
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 263 times
Been thanked: 129 times

how to add a lines of data to the top of the column?

Post by Zim »

Hi
I have a text file that I want to continue to add lines of data to, but I always want the added lines to insert at line 1. So, data6 should append to the top position and not erase data5, just move into its position.

<------ data6
data5
data4
data3
data2
data1

FILE.APPEND "/mydata.txt", chr$(10)+ "data6" + CR_LF$ maybe something like this????
Is it possible?


Thanks
Zim
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: how to add a lines of data to the top of the column?

Post by Fernando Perez »

You can try this:

Code: [Local Link Removed for Guests]

file$ = "/dateTime.log"
r = file.delete(file$)
line$ = "First line" + chr$(13)
file.append file$, line$

for i = 1 to 10
  line$ = date$ + " " + time$ + " " + str$(rnd(100)) + chr$(13)
  a$ = line$ + file.read$(file$)
  file.save file$, a$
  pause 1000
next i

wlog file.read$(file$)
But if the file is very large, it will take time to read it and will use a lot of memory.
Another point of view would be to use FILE.READ_IOBUFF, FILE.WRITE_IOBUFF and FILE.APPEND_IOBUFF, but apart from being more complicated to understand, I fear that you will also have the same memory problem with long files.
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: how to add a lines of data to the top of the column?

Post by Fernando Perez »

I remember something similar to this being discussed on [Local Link Removed for Guests] without it being very clear to me if there is a way to speed up this type of process in Annex.

Look at the timings and memory usage in this program:

Code: [Local Link Removed for Guests]

file$ = "/dateTime.log"
r = file.delete(file$)
line$ = "First line" + chr$(13)
file.append file$, line$
ramfree0 = ramfree
millis0 = millis

for i = 1 to 1000
  line$ = str$(i) + " " + str$(rnd(100)) + chr$(13)
  a$ = line$ + file.read$(file$)
  file.save file$, a$
next i

a$ = ""
ramfree1 = ramfree
millis1 = millis
wlog file.read$(file$)
ramfree2 = ramfree
millis2 = millis

wlog "start RAMFREE:   "; ramfree0, "start MILLIS:   "; millis0
wlog "between RAMFREE: "; ramfree1, "between MILLIS: "; millis1 
wlog "finish RAMFREE:  "; ramfree2, "finish MILLIS:  "; millis2 
wlog "used time: "; millis2 - millis0
wlog "used memory: "; ramfree0 - ramfree2

And notice how memory is freed by deleting the a$ variable after use:
image.png
You do not have the required permissions to view the files attached to this post.
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: how to add a lines of data to the top of the column?

Post by Fernando Perez »

I have prepared a subroutine on this topic, so that it is more easily usable in our future programs.
For this I have used the iobuffer, a topic perhaps more difficult to understand, but which seems more powerful to me for many purposes.
And I was surprised that the memory it uses seems not to be recovered correctly with the iobuffer.destroy instruction.
Is there something wrong with this instruction or am I not using it correctly?

Code: [Local Link Removed for Guests]

wlog ramfree
for i = 1 to 3
  file$ = "/dateTime.log"
  line$ = "new line at " + time$ + " of " + date$ + chr$(13)
  LIFO line$, file$
  pause 1000
next i

a$ = file.read$(file$)
wlog a$
a$ = ""  
wlog ramfree

' -------------------
SUB LIFO(reg$, file$)
LOCAL sizeLine, sizeBuffer0, sizeBuffer1
  sizeLine = len(reg$)
  file.read_iobuff(0), file$
  sizeBuffer0 = iobuff.len(0)
  iobuff.dim(1, sizeLine + sizeBuffer0)
  iobuff.fromString(1, reg$)
  iobuff.copy(1, sizeLine) , (0, 0, sizeBuffer0)
  file.write_iobuff(1), file$
  iobuff.destroy(0)
  iobuff.destroy(1)
END SUB
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 263 times
Been thanked: 129 times

Re: how to add a lines of data to the top of the column?

Post by Zim »

Hi Fernando Perez
Thanks a bunch! Your example works perfect for my purpose!

Code: [Local Link Removed for Guests]

mydata$ = "test"
file$ = "/time.txt"
line$ = chr$(13)
file.append file$, line$
line$ = mydata$ + chr$(13)
a$ = line$ + file.read$(file$)
file.save file$, a$
Thanks again
Zim
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: how to add a lines of data to the top of the column?

Post by Fernando Perez »

Thanks to you, Zim.
And I want to correct my previous post about iobuff.destroy malfunctioning.
I must have been doing something wrong because this program works perfectly for me. Both on an ESP8266 and an ESP32. Be patient, because it takes a while to write the 500 registers:

Code: [Local Link Removed for Guests]

wlog "*** RAMFREE AT STAR = "; ramfree
file$ = "/lifo.log"
if file.exists(file$) then r = file.delete(file$)
file.save file$, "First Line" + chr$(13)

for i = 1 to 500
  line$ = str$(i) + "=" + str$(ramfree) + chr$(13)
  LIFO line$, file$
next i

a$ = file.read$(file$)
wlog a$
a$ = ""  

wlog "*** RAMFREE at END = "; ramfree
END

' -------------------
SUB LIFO(reg$, file$)
LOCAL sizeLine, sizeBuffer0, sizeBuffer1
  sizeLine = len(reg$)
  file.read_iobuff(0), file$
  sizeBuffer0 = iobuff.len(0)
  iobuff.dim(1, sizeLine + sizeBuffer0)
  iobuff.fromString(1, reg$)
  iobuff.copy(1, sizeLine) , (0, 0, sizeBuffer0)
  file.write_iobuff(1), file$
  iobuff.destroy(0)
  iobuff.destroy(1)
END SUB
The ramfree is almost the same at the beginning as at the end.

Fun FACT: If I forget to put the END instruction at the end of the program, the ramfree is less.
Post Reply