' tb-screen: 80x25
' tb-tempdir
' Datei-E/A (Spec datei-eio): sequenziell schreiben und lesen, wahlfreier
' Zugriff mit UDT-Records und mit FIELD/LSET/RSET, Binaerzugriff sowie die
' Status- und Dateisystemfunktionen. Laeuft in einem temporaeren Verzeichnis.
TYPE Kunde
    Nr AS INTEGER
    Name AS STRING * 8
    Saldo AS DOUBLE
END TYPE
DIM k AS Kunde, g AS Kunde

' --- sequenziell ---
OPEN "seq.txt" FOR OUTPUT AS #1
PRINT #1, "Zeile eins"
PRINT #1, 1; 2; 3
WRITE #1, "a,b", 5
CLOSE #1
OPEN "seq.txt" FOR INPUT AS #1
PRINT "Groesse:"; LOF(1)
LINE INPUT #1, z$
PRINT "["; z$; "]"
LINE INPUT #1, z$
PRINT "["; z$; "]"
INPUT #1, s$, n%
PRINT "["; s$; "]"; n%; "EOF:"; EOF(1)
CLOSE #1

' --- Anhaengen ---
OPEN "seq.txt" FOR APPEND AS #1
PRINT #1, "angehaengt"
CLOSE #1
OPEN "seq.txt" FOR INPUT AS #1
anzahl% = 0
DO WHILE NOT EOF(1)
    LINE INPUT #1, z$
    anzahl% = anzahl% + 1
LOOP
PRINT "Zeilen:"; anzahl%; "letzte: ["; z$; "]"
CLOSE #1

' --- wahlfrei mit UDT ---
OPEN "rec.dat" FOR RANDOM AS #2 LEN = 42
k.Nr = 7
k.Name = "Mueller"
k.Saldo = 100
PUT #2, 3, k
GET #2, 3, g
PRINT g.Nr; "["; g.Name; "]"; g.Saldo; "LOF:"; LOF(2)
CLOSE #2

' --- wahlfrei mit FIELD ---
OPEN "fld.dat" FOR RANDOM AS #3 LEN = 80
FIELD #3, 10 AS nm$, 10 AS ort$
LSET nm$ = "Meier"
RSET ort$ = "Berlin"
PUT #3, 1
LSET nm$ = "Schulz"
LSET ort$ = "Hamburg"
PUT #3, 2
GET #3, 1
PRINT "["; nm$; "]["; ort$; "]"
GET #3, 2
PRINT "["; nm$; "]["; ort$; "]"
CLOSE #3

' --- binaer ---
OPEN "bin.dat" FOR BINARY AS #4
w% = 4711
PUT #4, 4, w%
w% = 0
GET #4, 4, w%
PRINT "binaer:"; w%; "SEEK:"; SEEK(4)
CLOSE #4

' --- Konvertierungsfunktionen ---
PRINT CVI(MKI$(-5)); CVL(MKL$(123456)); CVD(MKD$(1.5))
PRINT LEN(MKI$(1)); LEN(MKL$(1)); LEN(MKD$(1))

' --- Dateisystem ---
MKDIR "unter"
PRINT "Verzeichnis:"; LEN(CURDIR$) > 0
n$ = DIR$("*.dat")
anzahl% = 0
DO WHILE n$ <> ""
    anzahl% = anzahl% + 1
    n$ = DIR$("")
LOOP
PRINT "dat-Dateien:"; anzahl%
NAME "seq.txt" AS "neu.txt"
PRINT "umbenannt:"; DIR$("neu.txt") <> ""
KILL "neu.txt"
KILL "rec.dat"
KILL "fld.dat"
KILL "bin.dat"
RMDIR "unter"

' --- Fehlerfaelle ---
ON ERROR GOTO Fehler
OPEN "fehlt.txt" FOR INPUT AS #5
W1:
OPEN "a.txt" FOR OUTPUT AS #6
OPEN "b.txt" FOR OUTPUT AS #6
W2:
x& = LOF(9)
W3:
CLOSE #6
KILL "a.txt"
ON ERROR GOTO 0
PRINT "fertig"
END
Fehler:
PRINT "Fehler"; ERR
f% = f% + 1
SELECT CASE f%
    CASE 1: RESUME W1
    CASE 2: RESUME W2
    CASE ELSE: RESUME W3
END SELECT
