The WriteChrAt command will write a String of characters to any position within an open file channel.
FACTS:
* Writing none sequential data to a file can be very slow * WriteChrAt only works with files opened with WriteFile or OpenFile * WriteChrAt is for random access writing. * Strings will be written without any termination characters, unless you set the Length parameter to zero. In this case it will save append a zero byte.
Mini Tutorial:
This example creates a test file using the random access commands, then reads it back again using the random access file commands.
; Create a file name File$=TempDir$()+"PB_TestFile.txt" ; Get a Free File Channel Index MyFile=GetFreeFile() ; CReate a File for writing data to WriteFile File$,MyFile ; Write some Byte data at byte pos 100 WriteByteAt 1,100,255 ; Write some Word data at byte pos 200 WriteWordAt 1,200,64000 ; Write some Integer data at byte pos 300 WriteIntAt 1,300,111222 ; Write some Float data at byte pos 400 WriteFloatAt 1,400,123.456 ; Write some Stringt data at byte pos 500 WriteStringAt 1,500,"Play Basic" ; Write some String data at byte pos 600 WriteChrAt 1,600,"Hello World",11 ; Close this file channel CloseFile MyFile Print "============================" Print "Reading File Data:"+File$ Print "============================" ; Get a Free File Channel Index MyFile=GetFreeFile() ; Open a file to READ with READFILE ReadFile File$,MyFile ; Read and display byte from position 100 in this file Print ReadByteAt(myFile,100) ; Read and display the Word from position 200 in this file Print ReadWordAt(myFile,200) ; Read and display the Integer from position 300 in this file Print ReadIntAt(myFile,300) ; Read and display the Float from position 400 in this file Print ReadFloatAt(myFile,400) ; Read and display the String from position 500 in this file Print ReadStringAt$(myFile,500) ; Read and display the String from position 600 in this file Print ReadChrAt$(myFile,600,11) ; Close this file channel CloseFile MyFile ; Display the Size of the FIle Print "============================" Print "File Size:"+Str$(FileSize(file$)) Print "============================" If FileExist(file$) Then DeleteFile file$ ; Display the screen and wait for a key press Sync WaitKey |
This example would output. ============================ Reading File:C:Windows\temp\Pb_TestFile.txt ============================ 255 64000 111222 123.456 Play Basic Hello World ============================ File Size:611 ============================ |
|