File_WriteByte()

Parent Previous Next

File_WriteByte(pBytein)


pBytein: The byte you want to write to the file.


Write a Single Byte to an Open File


The File_WriteByte() procedure writes a single byte to an open file. To write to the n-th byte of the file, you need to call this function in a loop and first read up to the n-1-th byte with File_ReadByte().


How it Works:

• File Open Check: Before writing to a file, you must open it using File_Open(), File_Append(), or File_OpenAt(). This command will open the file and position the pointer at the end of the file for appending new data.

• Disk Full Check: You can monitor the Disk_tIsFull variable to detect when the SD card's storage is full. When Disk_tIsFull = 1, the memory is full, and no more data can be written to the SD card.

• File Open Check: Before writing to a file, you need to check if a file is currently open. This can be done by monitoring the File_tIsOpen variable, where File_tIsOpen = 1 indicates the file is open and available for writing.

• End of File (EOF): The File_tEOF variable helps track whether you have reached the end of the file. If File_tEOF = 1, it indicates you've reached the end of the file. This is important because if you're writing data after the last byte, you'll need to append or overwrite based on the file's current position.


Example:

File_WriteByte("A")


Notes:

• If you're writing to the n-th byte, you need to read the previous bytes first. You can loop through and use File_ReadByte() to read up to the n-2-th byte, then call File_WriteByte() for the byte you want to write.

• Ensure you check for a Disk_tIsFull condition before writing to avoid writing past the available storage space.

• Once a file is open and data is written, it remains open until closed using File_Close().


TftSetUp(0)                                                                                 'Set screen

TftResetScreen(BLACK)                                                                       'Reset screen

TftSetSndFont(0,0)                                                                          'Set Font0

While File_Init() <> cErrOK : Wend                                                          'Initialize SD Card

TftPrintSndString("Sd Initialization OK",1,0,0,1,GREEN,BLACK,0)                             'Confirm Initialize SD Card

File_TimeModified(5,11,24, 12,39,45)                                                        'Set modified property

While File_Append("append.log") <> cErrOK : Wend                                            'Open file append.log to edit data

TftPrintSndString("Append append.log OK",1,0,2,1,GREEN,BLACK,0)                             'Confirm append.log is open 

sString = " world!"                                                                         'Load string with data to be written

For bCounter = 0 To 12    

    If sString[bCounter] = 0 Then Break                                                     'break if end of string

    File_WriteByte(sString[bCounter])                                                       'write data to append.Log

Next

If File_Flush() = 1 Then 

    TftPrintSndString("Flush append.Log OK",1,0,3,1,GREEN,BLACK,0)                          'confirm flush ok and good to remove card

Else

    TftPrintSndString("Flush append.Log NO",1,0,3,1,GREEN,BLACK,0)                          'flush not ok and not good to remove card

EndIf 






Created with the Personal Edition of HelpNDoc: Full-featured EBook editor