TTxtFile - Line-Oriented Text File
Source: source/classes/ttxtfile.prg
TTxtFile inherits from TFile and provides line-oriented text file reading and writing
with INI-style section support. It buffers the entire file into memory and offers cursor-based
navigation (Advance, Rewind, Skip, GoTo), line-level operations (ReadLine, Add, AppendLn,
DelLine, InsLine, RepLine), and INI section key/value access (GetValue, SetValue).
Inheritance
classDiagram
TFile <|-- TTxtFile
class TTxtFile {
+cLine
+nTLines
+nLine
+Advance()
+Rewind()
+Skip(n)
+ReadLine()
+Add(c)
+AppendLn(c)
+DelLine()
+InsLine(c)
+RepLine(c)
+GoTop()
+GoBottom()
+GoTo(n)
+GetValue(cSec, cKey, xDef)
+SetValue(cSec, cKey, xVal)
}
DATA Members
| DATA | Type | Description |
|---|---|---|
cLine | Character | Current line content at the cursor position |
nTLines | Numeric | Total number of lines in the file |
nLine | Numeric | Current line position (1-based) |
Methods
| Method | Description |
|---|---|
Advance() | Move to the next line. Returns .T. if successful, .F. if at EOF |
Rewind() | Move to the previous line. Returns .T. if successful, .F. if at BOF |
Skip( n ) | Move forward (positive) or backward (negative) n lines |
ReadLine() | Return the current line content as a string |
Add( c ) | Append line c at the end of the file |
AppendLn( c ) | Insert line c after the current line |
DelLine() | Delete the current line |
InsLine( c ) | Insert line c before the current line |
RepLine( c ) | Replace the current line with c |
GoTop() | Move cursor to the first line |
GoBottom() | Move cursor to the last line |
GoTo( n ) | Move cursor to absolute line number n |
GetValue( cSec, cKey, xDef ) | Return the value of a key in an INI-style section, or xDef if missing |
SetValue( cSec, cKey, xVal ) | Set the value of a key in an INI-style section (creates section/key if needed) |
Example: Config File Reader
#include "FiveWin.ch"
function Main()
local oTxt := TTxtFile():New( "config.ini", 2 )
if oTxt:hFile != nil
// Read values from INI-style sections
? "Server:", oTxt:GetValue( "Database", "Server", "localhost" )
? "Port:", oTxt:GetValue( "Database", "Port", 3306 )
? "User:", oTxt:GetValue( "Database", "User", "admin" )
// Update or add a value
oTxt:SetValue( "Database", "LastAccess", DToC( Date() ) )
// Navigate lines manually
oTxt:GoTop()
while ! oTxt:lEof()
? oTxt:ReadLine()
oTxt:Advance()
endwhile
oTxt:Close()
else
? "File not found"
endif
return nil