Quick start manual

8-6
Delphi Language Guide
Handling null-terminated strings
The InOut function
The InOut function is called by the Read, Readln, Write, Writeln, Eof, Eoln, SeekEof,
SeekEoln, and CloseFile standard routines whenever input or output from the device is
required.
When Mode is fmInput, the InOut function reads up to BufSize characters into BufPtr^,
and returns the number of characters read in BufEnd. In addition, it stores zero in
BufPos. If the InOut function returns zero in BufEnd as a result of an input request, Eof
becomes True for the file.
When Mode is fmOutput, the InOut function writes BufPos characters from BufPtr^,
and returns zero in BufPos.
The Flush function
The Flush function is called at the end of each Read, Readln, Write, and Writeln. It can
optionally flush the text file buffer.
If Mode is fmInput, the Flush function can store zero in BufPos and BufEnd to flush the
remaining (unread) characters in the buffer. This feature is seldom used.
If Mode is fmOutput, the Flush function can write the contents of the buffer exactly like
the InOut function, which ensures that text written to the device appears on the
device immediately. If Flush does nothing, the text doesn’t appear on the device until
the buffer becomes full or the file is closed.
The Close function
The Close function is called by the CloseFile standard procedure to close a text file
associated with a device. (The Reset, Rewrite, and Append procedures also call Close if
the file they are opening is already open.) If Mode is fmOutput, then before calling
Close, the file system calls the InOut function to ensure that all characters have been
written to the device.
Handling null-terminated strings
The Delphi language’s extended syntax allows the Read, Readln, Str, and Val standard
procedures to be applied to zero-based character arrays, and allows the Write,
Writeln, Val, AssignFile, and Rename standard procedures to be applied to both zero-
based character arrays and character pointers. In addition, the following functions
are provided for handling null-terminated strings. For more information about null-
terminated strings, see “Working with null-terminated strings” on page 5-14.
Table 8.2 Null-terminated string functions
Function Description
StrAlloc Allocates a character buffer of a given size on the heap.
StrBufSize Returns the size of a character buffer allocated using StrAlloc or StrNew.
StrCat Concatenates two strings.
StrComp Compares two strings.