SD Card Read position

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
Gunplumber
Posts: 38
Joined: Wed Nov 02, 2022 10:31 am

SD Card Read position

Post by Gunplumber » Tue Aug 29, 2023 10:32 am

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

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SD Card Read position

Post by Jerry Messina » Tue Aug 29, 2023 11:49 am

With the file already open, you can use FSeek() to position to a random position.

Gunplumber
Posts: 38
Joined: Wed Nov 02, 2022 10:31 am

Re: SD Card Read position

Post by Gunplumber » Sat Sep 02, 2023 12:46 am

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

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SD Card Read position

Post by Jerry Messina » Sat Sep 02, 2023 1:52 pm

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

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SD Card Read position

Post by Jerry Messina » 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.

Gunplumber
Posts: 38
Joined: Wed Nov 02, 2022 10:31 am

Re: SD Card Read position

Post by Gunplumber » Sun Sep 03, 2023 2:34 am

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

Post Reply