Page 1 of 1

SD Card Read position

Posted: Tue Aug 29, 2023 10:32 am
by Gunplumber
Hi all

I am using the SD card module in my project which is working very well. I have the need to ready from a certain position within a file, but notice the read commands appear to start at byte zero and auto increment with every subsequent read command.. Is the a way to specify a starting position (in byes) within a file to begin reading from?

Cheers
Lee

Re: SD Card Read position

Posted: Tue Aug 29, 2023 11:49 am
by Jerry Messina
With the file already open, you can use FSeek() to position to a random position.

Re: SD Card Read position

Posted: Sat Sep 02, 2023 12:46 am
by Gunplumber
Thanks Gerry
With regard to FSeek() and FilePtr(), do these reference from position 0 or from position 1?
IE does FSeek(0) reference the first byte in a file? Or is it FSeek(1)?

Also does FilePtr() reference the current position, or the next position?

IE
SD.openfile()
Var=SD.readbyte()
longVAR=FilePtr()

What would Long var be at this point?

Cheers
Lee

Re: SD Card Read position

Posted: Sat Sep 02, 2023 1:52 pm
by Jerry Messina
FSeek() and FilePtr() are 0-based, so the first byte is at position 0.
Also does FilePtr() reference the current position, or the next position?
FilePtr() returns the value of SD.File.BytesRead

When you open a file, File.BytesRead is set = 0.
Each time you read or write a byte, File.BytesRead gets incremented.
The function FSeek() sets a new value for File.BytesRead.

So...

Code: Select all

SD.openfile()          // sets File.BytesRead=0
longVAR=FilePtr()      // longVAR would be 0

Var=SD.readbyte()      // returns current byte and increments File.BytesRead, so now File.BytesRead = 1
longVAR=FilePtr()      // longVAR would be 1

Re: SD Card Read position

Posted: Sat Sep 02, 2023 10:21 pm
by Jerry Messina
I noticed in the comments that you have to use OpenFileRW() instead of OpenFile() if you want to use FSeek().

I haven't tested that... if you run across anything odd let me know.

Re: SD Card Read position

Posted: Sun Sep 03, 2023 2:34 am
by Gunplumber
Jerry Messina wrote:
Sat Sep 02, 2023 10:21 pm
I noticed in the comments that you have to use OpenFileRW() instead of OpenFile() if you want to use FSeek().

I haven't tested that... if you run across anything odd let me know.
Thanks Jerry
I will have to test that..

Cheers
Lee