File
Reading and writing files. A file is opened with a number of your choice (0, 1, 2, …), or with #PB_Any, which returns a number to use instead.
Reads and writes go through a 64 KB buffer, so reading or writing a line or a number at a time is fast. Text is UTF-8 unless another format is given.
ReadFile ✅
Result = ReadFile(#File, Filename$ [, Flags])
Opens an existing file for reading.
CreateFile ✅
Result = CreateFile(#File, Filename$ [, Flags])
Creates a new, empty file for writing (and reading). An existing file is replaced.
OpenFile ✅
Result = OpenFile(#File, Filename$ [, Flags])
Opens a file for reading and writing, creating it if it doesn't exist. With #PB_File_Append in Flags, writing starts at the end.
For all three:
| Parameter | Meaning |
|---|---|
#File |
the number to use for this file, or #PB_Any |
Filename$ |
the file, relative to the current directory unless a full path is given |
Flags |
#PB_Ascii, #PB_UTF8 (default) or #PB_Unicode: the format of strings, combined with #PB_File_SharedWrite (others may write while it is open) or #PB_File_Append (OpenFile) |
Returns nonzero on success (with #PB_Any, the file's number), 0 if the file can't be opened.
CloseFile ✅
CloseFile(#File)
Writes anything still in the buffer, and closes the file. Opening a file with a number that is in use closes the old file first.
ReadString ✅
Text$ = ReadString(#File [, Flags [, Length]])
Reads one line. The end of the line (CR LF, LF or CR) isn't included in the result. Flags can give the format (otherwise the file's), and #PB_File_IgnoreEOL to read past line ends. Length limits the number of characters.
WriteString ✅
WriteString(#File, Text$ [, Format])
Writes text, in the file's format unless Format gives another.
WriteStringN ✅
WriteStringN(#File, Text$ [, Format])
As WriteString, followed by a line end (CR LF).
OpenConsole()
If CreateFile(0, "example.txt")
WriteStringN(0, "first line")
WriteStringN(0, "café")
WriteLong(0, 12345)
CloseFile(0)
EndIf
If ReadFile(0, "example.txt")
PrintN(ReadString(0))
PrintN(ReadString(0))
PrintN(Str(ReadLong(0)))
PrintN(Str(Lof(0)) + " bytes, at the end: " + Str(Eof(0)))
CloseFile(0)
EndIf
DeleteFile("example.txt")
Output:
first line
café
12345
23 bytes, at the end: 1
Eof ✅
Result = Eof(#File)
Nonzero when everything has been read.
OpenConsole()
f = CreateFile(#PB_Any, "numbers.txt")
For i = 1 To 3
WriteStringN(f, Str(i * i))
Next
CloseFile(f)
f = ReadFile(#PB_Any, "numbers.txt")
total = 0
While Not Eof(f)
total + Val(ReadString(f))
Wend
CloseFile(f)
DeleteFile("numbers.txt")
PrintN(Str(total))
Output:
14
ReadStringFormat ✅
Result = ReadStringFormat(#File)
Checks for a byte order mark at the start of the file and skips it. Returns #PB_UTF8, #PB_Unicode, #PB_UTF16BE, #PB_UTF32 or #PB_UTF32BE, or #PB_Ascii when there is none. A detected format becomes the file's format for later ReadString calls.
WriteStringFormat ✅
WriteStringFormat(#File, Format)
Writes a byte order mark (#PB_UTF8 or #PB_Unicode), and makes that the file's format.
OpenConsole()
f = CreateFile(#PB_Any, "bom.txt")
WriteStringFormat(f, #PB_UTF8)
WriteString(f, "naïve")
CloseFile(f)
f = ReadFile(#PB_Any, "bom.txt")
If ReadStringFormat(f) = #PB_UTF8
PrintN("UTF-8: " + ReadString(f))
EndIf
CloseFile(f)
DeleteFile("bom.txt")
Output:
UTF-8: naïve
ReadData ✅
Result = ReadData(#File, *Memory, Length)
Reads up to Length bytes into memory. Returns the number of bytes read.
WriteData ✅
Result = WriteData(#File, *Memory, Length)
Writes Length bytes from memory.
Lof ✅
Length.q = Lof(#File)
The file's length in bytes.
Loc ✅
Position.q = Loc(#File)
The current read/write position.
FileSeek ✅
FileSeek(#File, Position.q [, Mode])
Moves the read/write position: to Position (#PB_Absolute, the default), or by Position bytes (#PB_Relative).
OpenConsole()
f = CreateFile(#PB_Any, "data.bin")
For i = 0 To 9
WriteByte(f, i * 3)
Next
CloseFile(f)
f = ReadFile(#PB_Any, "data.bin")
size = Lof(f)
*mem = AllocateMemory(size)
ReadData(f, *mem, size)
FileSeek(f, 4)
PrintN(Str(size) + " " + Str(PeekA(*mem + 9)) + " " + Str(ReadByte(f)) + " " + Str(Loc(f)))
CloseFile(f)
FreeMemory(*mem)
DeleteFile("data.bin")
Output:
10 27 12 5
Reading and writing numbers ✅
Number = ReadByte(#File) WriteByte(#File, Number)
Number = ReadWord(#File) WriteWord(#File, Number)
Number = ReadLong(#File) WriteLong(#File, Number)
Number = ReadQuad(#File) WriteQuad(#File, Number)
Number = ReadInteger(#File) WriteInteger(#File, Number)
Number = ReadFloat(#File) WriteFloat(#File, Number)
Number = ReadDouble(#File) WriteDouble(#File, Number)
Number = ReadAsciiCharacter(#File)
Number = ReadUnicodeCharacter(#File)
Each reads or writes one value of that type, in its binary form (little-endian).
IsFile ✅
Result = IsFile(#File)
Nonzero if the number is an open file.
FlushFileBuffers ✅
FlushFileBuffers(#File)
Writes anything waiting in the buffer now.
ReadCharacter, WriteCharacter ✅
Character = ReadCharacter(#File [, Format])
WriteCharacter(#File, Character [, Format])
WriteAsciiCharacter(#File, Character)
WriteUnicodeCharacter(#File, Character)
One character in #PB_Unicode (2 bytes, the default), #PB_Ascii (1) or #PB_UTF8 (1 to 4).
TruncateFile ✅
Result = TruncateFile(#File)
Cuts the file off at the current position.
FileID, FileBuffersSize ✅
Handle = FileID(#File)
FileBuffersSize(#File, Size)
FileID returns the system's handle for the file (a file descriptor on Linux). FileBuffersSize writes out the data waiting in the buffer.
Not yet available 🔜
#PB_File_NoBuffering and FileBuffersSize with a different size (accepted, but the buffer stays 64 KB; FileBuffersSize writes out what is waiting).