Page 1 of 2

Q about setPos

Posted: Wed Dec 08, 2021 9:46 pm
by WingNut
I want to write a number to the very first bytes of a file. The size of the number isn't important but won't be more than ten digits.
What is the handle? Is the address 0? and is the 'from' also 0?

Re: Q about setPos

Posted: Thu Dec 09, 2021 6:44 pm
by WingNut
Go on give me a clue

Re: Q about setPos

Posted: Thu Dec 09, 2021 7:21 pm
by stefan.erni
Hi WingNut

a simple way is with append

regards

Stefan
append_2021-12-09_20-15-10.png
append_2021-12-09_20-15-10.png (43.19 KiB) Viewed 4001 times

Re: Q about setPos

Posted: Thu Dec 09, 2021 8:36 pm
by WingNut
Thanks Stefan. I want to add data to specifically the first bytes of the file. Sort of the write equivalent of ReadStringFromFile

Re: Q about setPos

Posted: Fri Dec 10, 2021 11:20 am
by BenR
Hello,

I'm assuming you're using the FAT or FILE component and not the Simulation API.

Open file reads the first 512 byte sector of the file into ram.

Read Byte from buffer reads directly from the ram buffer, so you can read bytes 0 to 9 to get the first 10 characters of the file. Stores these into a string variable str[0] to str[9] and add a null termination at str[10].

Code: Select all

OpenFile("File.txt")
idx = 0
while (idx < 10)
{
  str[idx] = ReadByteFromBuffer(idx)
  idx = idx + 1
}
str[idx] = 0
Then convert str to an long.

Code: Select all

long = StringToInt$(Str)
Then do what you need to the long variable, e.g. add one and convert back into an string.

Code: Select all

str = ToString$(long)
You may need to padd the string with leading 0's or spaces to get it back to the right length.

Code: Select all

while (Length$(str) < 10)
{
  tempstr = "0" + str
  str = tempstr
}
Write byte to buffer allows you to write directly to the ram buffer so you can write bytes 0 to 9 from the string to set the first 10 characters of the file.

Code: Select all

idx = 0
while (idx < 10)
{
  WriteByteToBuffer(idx, str[idx])
  idx = idx + 1
}
str[idx] = 0
WriteFileSector()
Write file sector then writes the ram buffer back to the file.

Hope this helps, let us know how you're getting on.

Re: Q about setPos

Posted: Fri Dec 10, 2021 5:32 pm
by WingNut
Thanks Ben. I'll give that a go when I get another 2 issues sorted :o

Re: Q about setPos

Posted: Sun Dec 12, 2021 5:02 pm
by WingNut
So before I get into saving data to the card. Can anyone help with compiler errors I'm now getting. File attached

Re: Q about setPos

Posted: Sun Dec 12, 2021 5:54 pm
by WingNut
Update : I took out a goto component and put it back in exactly as it was and now it compiles! ;)

Re: Q about setPos

Posted: Tue Dec 21, 2021 8:36 pm
by WingNut
Hi Ben. So although i know c programming (somewhat) I'm not sure how to use this in flowcode. Is there tutorial available? I've looked and can't find anything with detail. I clearly cant just copy this and put it into a c code macro. Can variables declared in flowcode be accessed directly from a piece of c code or do they need to be declared in the block? Can you explain using your example?

Cheers
N

Re: Q about setPos

Posted: Sat Jan 01, 2022 9:46 pm
by WingNut
Hi again Ben. The attached is what i understand but i know it doesn't work. Can you give me a pointer?