Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete records in a random file
#1
The example is a random file that is connected to an index file. A kind of ISAM file management. The practical sense should be:
One cannot simply delete a record from a random file; For example, the data record with the item number (key) 2345 should be deleted.

To make this possible, entry 2345 in the index file must first be deleted, but the data record number, which is also saved and refers to the random file, remains intact. By deleting entry 2345 in the index file, all occupied entries in the index file move down, while the "deleted" entry with the data record number is stored as the first free field above it. The effect is that if a new data record is written, then the "deleted" data record is overwritten. In this respect, a random data record has been deleted after all. - So much for the theory.

The template for the example is from a book. There is no error message, but the result is not convincing!  Sad  I can't figure out why nothing useful is displayed. But there must be a logical error somewhere.

Maybe someone could look at the whole thing and find the error. Thanks!

Create random file:
Code: (Select All)

'Randomdatei mit Index, Hueckstaedt S. 321 - 13. Mai 2024

$Console:Only

Option _Explicit

Type Warenposten
  nummer As String * 4
  artikel As String * 10
  preis As Double
End Type

'Variable von Warenposten
Dim datensatz As Warenposten

Declare Sub AddiereSchluessel(i As Integer, nummer As String, index() As String)

Dim As Integer maxAnzahl, i, k

'Indexdatei fuer maximal 20 Datensaetze anlegen
maxAnzahl = 10
Dim As String index(maxAnzahl)
For i = 1 To maxAnzahl
  index(i) = "9999":
Next i

'Randomdatei anlegen bzw. oeffnen.
'Len muss dem laengsten Datensatz entsprechen
Open "RandomIndex.dat" For Random As #5 Len = Len(datensatz)

i = 1
Do
  'Daten aus Datazeile einlesen
  Read datensatz.nummer
  Read datensatz.artikel
  Read datensatz.preis

  'Datensatz in Datei schreiben
  Put #5, i, datensatz

  'Index aktuallisiren
  Call AddiereSchluessel(i, datensatz.nummer, index())

  'Zeiger erhoehen
  i = i + 1

Loop Until datensatz.nummer = "9999"

'Datenbank schliesen
Close #5

'Indexdatei schreiben
Open "RandomIndex.ind" For Output As #3
For k = 1 To maxAnzahl
  Print #3, index(k)
Next k

Close #3
End
:

Data 1233,Lenker,245.99
Data 0891,Lampe,188.90
Data 1122,Telegabel,499.95
Data 2301,Tank,377.50
Data 1755,Sitzbank,321.00
Data 9999,"",0

Sub AddiereSchluessel (i As Integer, nummer As String, index() As String)

  Dim As String zahl, einfuegen
  Dim As Integer k

  zahl = " ": RSet zahl = Str$(i)

  k = 1: einfuegen = nummer + zahl

  Do While einfuegen > index(k) And k <= i
    k = k + 1
  Loop

  Do While k <= i
    Swap einfuegen, index(k)
    k = k + 1
  Loop

  index(k) = einfuegen
End Sub

Output of the data
Code: (Select All)

'Die erstellte Randomsatei mit Index lesen - Hueck S.324 - 26. Mai 2024

Cls

Option _Explicit

Type Warenposten
  nummer As String * 4
  artikel As String * 10
  preis As Double
End Type

'Variable von Warenposten
Dim datensatz As Warenposten

Dim As Integer maxAnzahl, i, k, z

'Indexdatei einlesen
maxAnzahl = 10
Dim As String index(maxAnzahl)

i = 0
Open "RandomIndex.ind" For Input As #3
Do While Not EOF(3) And index(i) <> "9999"
  i = i + 1
  Input #3, index(i)
Loop
Close #3

k = i - 1

'Randomdatei oeffnen
Open "RandomIndex.dat" For Random As #5 Len = Len(datensatz)
Print "Nr.    Artikel              Preis"
Print

i = 1
Do While Not i > k
  z = Val(Right$(index(i), 4))

  'Datensatz lesen
  Get #5, z, datensatz

  'Dateiende Kennzeichnung
  If datensatz.nummer = "9999" Then Exit Do

  'Datensatz ausgeben
  Print Using "\  \"; datensatz.nummer;
  Print "    ";
  Print Using "\              \"; datensatz.artikel;
  Print "    ";
  Print Using "####.##"; datensatz.preis

  'Zeiger erhoehen
  i = i + 1
Loop

Close #5

End 'Hauptprogramm

[Image: Random-Index-Datei-ausgeben2024-05-26.jpg]
Reply


Messages In This Thread
Delete records in a random file - by Kernelpanic - 05-26-2024, 05:37 PM
RE: Delete records in a random file - by SMcNeill - 05-26-2024, 05:54 PM



Users browsing this thread: 2 Guest(s)