====== Basic Turbo Delphi I/O Functions ====== ^ System File I/O ^^ |Append|Opens an existing text file for appending.| |AssignFile|Assigns the name of an external file to a file variable.| |BlockRead|Reads one or more records from an untyped file.| |BlockWrite|Writes one or more records into an untyped file.| |ChDir|Changes the current directory.| |CloseFile|Closes an open file.| |Eof|Returns the end-of-file status of a file.| |Eoln|Returns the end-of-line status of a text file.| |Erase|Erases an external file.| |FilePos|Returns the current file position of a typed or untyped file.| |FileSize|Returns the current size of a file; not used for text files.| |Flush|Flushes the buffer of an output text file.| |GetDir|Returns the current directory of a specified drive.| |IOResult|Returns an integer value that is the status of the last I/O function performed.| |MkDir|Creates a subdirectory.| |Read|Reads one or more values from a file into one or more variables.| |Readln|Does what Read does and then skips to beginning of next line in the text file.| |Rename|Renames an external file.| |Reset|Opens an existing file.| |Rewrite|Creates and opens a new file.| |RmDir|Removes an empty subdirectory.| |Seek|Moves the current position of a typed or untyped file to a specified component. Not used with text files.| |SeekEof|Returns the end-of-file status of a text file.| |SeekEoln|Returns the end-of-line status of a text file.| |SetTextBuf|Assigns an I/O buffer to a text file.| |Truncate|Truncates a typed or untyped file at the current file position.| |Write|Writes one or more values to a file.| |Writeln|Does the same as Write, and then writes an end-of-line marker to the text file.| \\ There are three classes of file: typed, text, and untyped. The syntax for declaring file types is given in File types. Note that file types are only available on the Win32 platform. Once the association with an external file is established, the file variable must be opened to prepare it for input or output. An existing file can be opened via the Reset procedure, and a new file can be created and opened via the Rewrite procedure. Text files opened with Reset are read-only and text files opened with Rewrite and Append are write-only. Typed files and untyped files always allow both reading and writing regardless of whether they were opened with Reset or Rewrite . By default, all calls to standard I/O procedures and functions are automatically checked for errors, and if an error occurs an exception is raised (or the program is terminated if exception handling is not enabled). This automatic checking can be turned on and off using the {$I+} and {$I-} compiler directives. When I/O checking is off, that is, when a procedure or function call is compiled in the {$I-} state an I/O error doesn't cause an exception to be raised; to check the result of an I/O operation, you must call the standard function IOResult instead. You must call the IOResult function to clear an error, even if you aren't interested in the error. If you don't clear an error and {$I-} is the current state, the next I/O function call will fail with the lingering IOResult error. ===== File I/O Using Handles: FileOpen Routine ===== Opens a specified file using a specified access mode. function FileOpen(const FileName: string; Mode: Cardinal): Integer; ** Mode ** * to_read ** Description ** Use FileOpen to open a file and obtain a file handle. The access mode value is constructed by or-ing one of the fmOpen constants with one of the fmShare constants defined in File open mode constants. If the return value is 0 or greater, the function was successful and the value is the file handle of the opened file. A return value of -1 indicates that an error occurred. var inf_hand : integer; {handle for the file} inf_hand := fileopen(CookieFileName,to_read); if inf_hand = -1 {if the handle comes back -1, it didn't work} then begin bombout('Couldn''t read fortune.'); end {then} else begin endptr := fileseek(inf_hand,0,frombottom); {find EOF} if locptr >= endptr then get_new_fortune_file; fileseek(inf_hand,locptr,fromtop); if IOresult <> 0 then bombout('Fileseek failed.'); ===== Text Files ===== This section summarizes I/O using file variables of the standard type Text. When a text file is opened, the external file is interpreted in a special way: It is considered to represent a sequence of characters formatted into lines, where each line is terminated by an end-of-line marker (a carriage-return character, possibly followed by a line feed character). The type Text is distinct from the type file of Char. |CR is the distinctive e-o-l char. LF is optional. For text files, there are special forms of Read and Write that let you read and write values that are not of type Char. Such values are automatically translated to and from their character representation. For example, Read(F, I), where I is a type Integer variable, reads a sequence of digits, interprets that sequence as a decimal integer, and stores it in I. ===== Read Routine ===== Read reads data from a file. [Delphi] procedure Read(var F: file; V1: Pointer [; V2; ...; Vn]); **Description ** The Read procedure can be used in Delphi code in the following ways.\\ For typed files, it reads a file component into a variable.\\ For text files, it reads one or more values into one or more variables.\\ Read reads all characters up to, but not including, the next end-of-line marker or until Eof(F) becomes true; it does not skip to the next line after reading. If the resulting string is longer than the maximum length of the string variable, it is truncated.\\ After the first Read, each subsequent Read sees the end-of-line marker and returns a zero-length string.\\ Use multiple Readln calls to read successive string values.\\ When the extended syntax is enabled, Read can read null-terminated strings into zero-based character arrays.\\ Read reads one character from the file and assigns it to the variable. If CRLF mode is enabled and Eof(F) was true before Read was executed, the value Chr(26) (a Ctrl-Z character) is assigned to the variable. (To enable CRLF mode, use SetLineBreakStyle.)\\ Read skips any blanks, tabs, or end-of-line markers preceding the numeric string.\\ If the numeric string does not conform to the expected format, an I/O error occurs; otherwise, the value is assigned to the variable.\\ The next Read starts with the blank, tab, or end-of-line marker that terminated the numeric string.\\ ** Related Information ** System.Eof \\ System.Readln \\ System.Write \\ System.Writeln \\ System.SetLineBreakStyle \\ ===== 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. ===== Null-Terminated String Functions ===== The following functions are provided for handling null-terminated strings. ^ Procedures and Functions in SysUtils: ^^ |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.| |StrCopy|Copies a string.| |StrDispose|Disposes a character buffer allocated using StrAlloc or StrNew.| |StrECopy|Copies a string and returns a pointer to the end of the string.| |StrEnd|Returns a pointer to the end of a string.| |StrFmt|Formats one or more values into a string.| |StrIComp|Compares two strings without case sensitivity.| |StrLCat|Concatenates two strings with a given maximum length of the resulting string.| |StrLComp|Compares two strings for a given maximum length.| |StrLCopy|Copies a string up to a given maximum length.| |StrLen|Returns the length of a string.| |StrLFmt|Formats one or more values into a string with a given maximum length.| |StrLIComp|Compares two strings for a given maximum length without case sensitivity.| |StrLower|Converts a string to lowercase.| |StrMove|Moves a block of characters from one string to another.| |StrNew|Allocates a string on the heap.| |StrPCopy|Copies a Pascal string to a null-terminated string.| |StrPLCopy|Copies a Pascal string to a null-terminated string with a given maximum length.| |StrPos|Returns a pointer to the first occurrence of a given substring within a string.| |StrRScan|Returns a pointer to the last occurrence of a given character within a string.| |StrScan|Returns a pointer to the first occurrence of a given character within a string.| |StrUpper|Converts a string to uppercase.| Standard string-handling functions have multibyte-enabled counterparts that also implement locale-specific ordering for characters. Names of multibyte functions start with Ansi-. For example, the multibyte version of StrPos is AnsiStrPos. Multibyte character support is operating-system dependent and based on the current locale. ===== Wide-Character Strings ===== The System unit provides three functions, WideCharToString , WideCharLenToString, and StringToWideChar, that can be used to convert null- terminated wide character strings to single- or double-byte long strings. Assignment will also convert between strings. For instance, the following are both valid: MyAnsiString := MyWideString; MyWideString := MyAnsiString; ===== Other Standard Routines ===== The table below lists frequently used procedures and functions found in Borland product libraries. This is not an exhaustive inventory of standard routines. |Addr|Returns a pointer to a specified object.| |AllocMem|Allocates a memory block and initializes each byte to zero.| |ArcTan|Calculates the arctangent of the given number.| |Assert|Raises an exception if the passed expression does not evaluate to true.| |Assigned|Tests for a nil (unassigned) pointer or procedural variable.| |Beep|Generates a standard beep.| |Break|Causes control to exit a for, while, or repeat statement.| |ByteToCharIndex|Returns the position of the character containing a specified byte in a string.| |Chr|Returns the character for a specified integer value.| |Close|Closes a file.| |CompareMem|Performs a binary comparison of two memory images.| |CompareStr|Compares strings case sensitively.| |CompareText|Compares strings by ordinal value and is not case sensitive.| |Continue|Returns control to the next iteration of for, while, or repeat statements.| |Copy|Returns a substring of a string or a segment of a dynamic array.| |Cos|Calculates the cosine of an angle.| |CurrToStr|Converts a currency variable to a string.| |Date|Returns the current date.| |DateTimeToStr|Converts a variable of type TDateTime to a string.| |DateToStr|Converts a variable of type TDateTime to a string.| |Dec|Decrements an ordinal variable or a typed pointer variable.| |Dispose|Releases dynamically allocated variable memory.| |ExceptAddr|Returns the address at which the current exception was raised.| |Exit|Exits from the current procedure.| |Exp|Calculates the exponential of X.| |FillChar|Fills contiguous bytes with a specified value.| |Finalize|ninitializes a dynamically allocated variable.| |FloatToStr|Converts a floating point value to a string.| |FloatToStrF|Converts a floating point value to a string, using specified format.| |FmtLoadStr|Returns formatted output using a resourced format string.| |FmtStr|Assembles a formatted string from a series of arrays.| |Format|Assembles a string from a format string and a series of arrays.| |FormatDateTime|Formats a date-and-time value.| |FormatFloat|Formats a floating point value.| |FreeMem|Releases allocated memory.| |GetMem|Allocates dynamic memory and a pointer to the address of the block.| |Halt|Initiates abnormal termination of a program.| |Hi|Returns the high-order byte of an expression as an unsigned value.| |High|Returns the highest value in the range of a type, array, or string.| |Inc|Increments an ordinal variable or a typed pointer variable.| |Initialize|Initializes a dynamically allocated variable.| |Insert|Inserts a substring at a specified point in a string.| |Int|Returns the integer part of a real number.| |IntToStr|Converts an integer to a string.| |Length|Returns the length of a string or array.| |Lo|Returns the low-order byte of an expression as an unsigned value.| |Low|Returns the lowest value in the range of a type, array, or string.| |LowerCase|Converts an ASCII string to lowercase.| |Math.MaxIntValue|Returns the largest signed value in an integer array.| |Math.MaxValue|Returns the largest signed value in an array.| |Math.MinIntValue|Returns the smallest signed value in an integer array.| |Math.MinValue|Returns smallest signed value in an array.| |New|Creates a dynamic allocated variable memory and references it with a specified pointer.| |Now|Returns the current date and time.| |Ord|Returns the ordinal integer value of an ordinal-type expression.| |Pos|Returns the index of the first single-byte character of a specified substring in a string.| |Pred|Returns the predecessor of an ordinal value.| |Ptr|Converts a value to a pointer.| |Random|Generates random numbers within a specified range.| |ReallocMem|Reallocates a dynamically allocatable memory.| |Round|Returns the value of a real rounded to the nearest whole number.| |SetLength|Sets the dynamic length of a string variable or array.| |SetString|Sets the contents and length of the given string.| |ShowException|Displays an exception message with its address. |Sin|Returns the sine of an angle in radians.| |SizeOf|Returns the number of bytes occupied by a variable or type.| |Sqr|Returns the square of a number.| |Sqrt|Returns the square root of a number.| |Str|Converts an integer or real number into a string.| |StrToCurr|Converts a string to a currency value.| |StrToDate|Converts a string to a date format (TDateTime).| |StrToDateTime|Converts a string to a TDateTime.| |StrToFloat|Converts a string to a floating-point value.| |StrToInt|Converts a string to an integer.| |StrToTime|Converts a string to a time format (TDateTime).| |StrUpper|Returns an ASCII string in upper case.| |Succ|Returns the successor of an ordinal value.| |Math.Sum|Returns the sum of the elements from an array.| |Time|Returns the current time.| |TimeToStr|Converts a variable of type TDateTime to a string.| |Trunc|Truncates a real number to an integer.| |UniqueString|Ensures that a string has only one reference. (The string may be copied to produce a single reference.)| |UpCase|Converts a character to uppercase.| |UpperCase|Returns a string in uppercase.| |Variants.VarArrayCreate|Creates a variant array.| |Variants.VarArrayDimCount|Returns number of dimensions of a variant array.| |Variants.VarArrayHighBound|Returns high bound for a dimension in a variant array.| |Variants.VarArrayLock|Locks a variant array and returns a pointer to the data.| |Variants.VarArrayLowBound|Returns the low bound of a dimension in a variant array.| |Variants.VarArrayOf|Creates and fills a one-dimensional variant array.| |VarArrayRedim|Resizes a variant array.| |Variants.VarArrayRef|Returns a reference to the passed variant array.| |Variants.VarArrayUnlock|Unlocks a variant array.| |Variants.VarAsType|Converts a variant to specified type.| |VarCast|Converts a variant to a specified type, storing the result in a variable.| |VarClear|Clears a variant.| |VarCopy|Copies a variant.| |Variants.VarToStr|Converts variant to string.| |Variants.VarType|Returns type code of specified variant.| ===== Show Message ===== |Dialogs.ShowMessage|Displays a message box with an unformatted string and an OK button.| |Dialogs.ShowMessageFmt|Displays a message box with a formatted string and an OK button.| ===== Extracting info from paths ===== |ExtractFileDir(FName)| |ExtractFileDrive(FName)| |ExtractFileExt(FName)| |ExtractFileName(FName)| |ExtractFilePath(FName)|