Sample Code 2 - Writing and overwriting a file using FSeek
Device = 18F4620
Clock = 40
Config OSC = HSPLL
#option SD_SPI = MSSP
#option SD_SPI_SPEED = spiOscDiv4
#option SD_SUPPORT_SUB_DIRECTORIES = False
Include "SDFileSystem.bas"
Include "USART.bas"
Include "Convert.bas"
Dim DoNotRemoveLED As PORTD.0
Dim XYSet As LongWord
Sub WriteFooter()
SD.Write(" </Coordinates>", 13, 10)
SD.Write(" </Document>", 13, 10)
SD.Write("</kml>", 13, 10)
End Sub
Output(DoNotRemoveLED)
DelayMS(100)
USART.SetBaudrate(br38400)
USART.Write("Insert SD/MMC:", 13, 10)
Repeat
Until SD.Init() = errOK
DoNotRemoveLED = 1
USART.Write("Formatting:", 13, 10)
SD.QuickFormat
USART.Write("WRITING HEADER & FOOTER...", 13, 10)
SD.NewFile("TESTFILE.XML")
SD.Write("<?xml version=", 34, "1.0", 34, " encoding=", 34, "UTF-8", 34, "?>", 13, 10)
SD.Write("<kml>", 13, 10)
SD.Write(" <Document>", 13, 10)
SD.Write(" <Coordinates>", 13, 10)
WriteFooter()
USART.Write("HEADER & FOOTER WRITTEN", 13, 10)
USART.Write("CLOSING FILE...", 13, 10)
SD.CloseFile
USART.Write("FILE CLOSED", 13, 10)
USART.Write("OPENING FILE...", 13, 10)
SD.OpenFileRW("TESTFILE.XML")
USART.Write("FILE OPENED", 13, 10)
DoNotRemoveLED = 0
XYSet = 0
Repeat
If XYSet Mod 100 = 0 Then
USART.Write("*")
DelayMS(3000)
EndIf
DoNotRemoveLED = 1
USART.Write(".")
SD.FSeek(SD.FileSize - 41)
SD.Write(" ", DecToStr(XYSet), ",", DecToStr(XYSet), 13, 10)
WriteFooter()
SD.SaveFile
DoNotRemoveLED = 0
Inc(XYSet)
Until False