UserGuide - Reading and writing files
This example will write 100 random records each containing a byte, a floating-point number, a long integer and a string. It then reads all the records back and displays them in the debug window. EnableExplicit
; Define the constants:
#MAXBYTE = 255
#MAXLONG = 2147483647
; Define some variables.
Define.f Float
Define.l Count, File
Define.s Folder, FileName, String
; Create a temporary file name.
Folder = GetTemporaryDirectory()
FileName = Folder + "test.data"
; Create the temporary file.
; If #PB_Any is used, CreateFile returns the file's number.
; Useful if you may have more than one file open simultaneously.
File = CreateFile(#PB_Any, FileName)
If File
; If this was successful - write 100 random records.
For Count = 1 To 100
; Write a random byte (0 - 255).
WriteByte(File, Random(#MAXBYTE))
; Create and write a random float.
; This calculation is there to make the number have a floating-point component (probably).
Float = Random(#MAXLONG) / ((Random(7) + 2) * 5000)
WriteFloat(File, Float)
; Write a random long.
WriteLong(File, Random(#MAXLONG))
; Create and write a random string in Unicode format.
; Note the use of WriteStringN to delimit the string with an end of line marker.
String = "String " + StrU(Random(#MAXLONG))
WriteStringN(File, String, #PB_Unicode)
Next Count
; Close the file.
CloseFile(File)
Else
; If this was unsuccessful.
Debug "Could not create the file: " + FileName
EndIf
; Open the file for reading this time.
File = ReadFile(#PB_Any, FileName)
If File
; If this was successful - read and display records from the file.
; Reset the counter.
Count = 1
; Loop until the 'end of file' is reached.
; This will read all of the records regardless of how many there are.
While Eof(File) = 0
; Print a header line.
Debug "------------------------------------------------------------------------------------------------"
Debug "[" + StrU(Count) + "]"
Count + 1
; Read a byte value and print it.
Debug StrU(ReadByte(File), #PB_Byte)
; Read a float value.
Debug StrF(ReadFloat(File))
; Read a long value.
Debug StrU(ReadLong(File), #PB_Long)
; Read a string value.
Debug ReadString(File, #PB_Unicode)
Wend
; Print the trailing line.
Debug "------------------------------------------------------------------------------------------------"
; Close the file.
CloseFile(File)
; Tidy up.
DeleteFile(FileName)
Else
; If this was unsuccessful.
Debug "Could not open the file: " + FileName
EndIf
UserGuide Navigation
< Previous: Compiler directives | Overview | Next: Memory access >