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
how to add a lines of data to the top of the column?
- 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?
You can try this:
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.
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$)
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.
- 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?
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:
And notice how memory is freed by deleting the a$ variable after use:
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
You do not have the required permissions to view the files attached to this post.
- 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?
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?
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
Re: how to add a lines of data to the top of the column?
Hi Fernando Perez
Thanks a bunch! Your example works perfect for my purpose!
Thanks again
Zim
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$
Zim
- 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?
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:
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.
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
Fun FACT: If I forget to put the END instruction at the end of the program, the ramfree is less.