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?
Q about setPos
-
- Posts: 262
- http://meble-kuchenne.info.pl
- Joined: Tue Jul 13, 2021 1:53 pm
- Has thanked: 35 times
- Been thanked: 27 times
-
- Valued Contributor
- Posts: 893
- Joined: Wed Dec 02, 2020 10:53 am
- Has thanked: 175 times
- Been thanked: 206 times
-
- Matrix Staff
- Posts: 1901
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 494 times
- Been thanked: 668 times
Re: Q about setPos
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].
Then convert str to an long.
Then do what you need to the long variable, e.g. add one and convert back into an string.
You may need to padd the string with leading 0's or spaces to get it back to the right length.
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.
Write file sector then writes the ram buffer back to the file.
Hope this helps, let us know how you're getting on.
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
Code: Select all
long = StringToInt$(Str)
Code: Select all
str = ToString$(long)
Code: Select all
while (Length$(str) < 10)
{
tempstr = "0" + str
str = tempstr
}
Code: Select all
idx = 0
while (idx < 10)
{
WriteByteToBuffer(idx, str[idx])
idx = idx + 1
}
str[idx] = 0
WriteFileSector()
Hope this helps, let us know how you're getting on.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Re: Q about setPos
So before I get into saving data to the card. Can anyone help with compiler errors I'm now getting. File attached
- Attachments
-
- Flowcode1.msg.txt
- (3.15 KiB) Downloaded 222 times
Re: Q about setPos
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
Cheers
N