Random Access with a little problem
#1
Wink 
I have now created a "Random Access" data structure (German: Direktzugriffsdatei). Seems to work. There are three records in the file.

But there is one point I don't understand: 137: If sentenceNumber > 0 And sentenceNumber < number of sentences + 1 Then

Why plus 1? The data sets do not start at zero, otherwise data set 1 would show that of data set 2. It is working.
I have to take a good look at the deletion of data records again. Let's see.


Oh yes, a problem with the output. Is there a way to add vertical scroll bars? Making the output bigger doesn't help. How are you supposed to keep track of 100 data sets?

Code: (Select All)
'Direktzugriffsdatei (Random Access) - 5. Okt. 2022
'Geaendert auf "Shared" Variable da sonst Probleme beim Lesen - 14. Okt. 2022

Option _Explicit

'Definition der Datenstruktur - Direktzugriff
Type MotorradModell
  Modell As String * 20
  Farbe As String * 10
  Hubraum As String * 10
  Kilowatt As String * 10
  Fahrgewicht As String * 10
  Preis As Double
End Type

'Global zur Verfuegung stellen, sonst wird es
'wirklich kompliziert
Dim Shared Motorrad As MotorradModell

Declare Sub Eingabe()
Declare Sub Lesen()
Declare Sub SatzLesen()

Dim As Integer auswahl

Nochmal:
Cls
auswahl = 0
Locate 3, 4
Print "Waehlen Sie das gewuenschte Programm."
Locate 6, 10
Print "In Datei schreiben    -> 1"
Locate 7, 10
Print "Datei lesen           -> 2"
Locate 8, 10
Print "Bestimmten Satz lesen -> 3"
Locate 9, 10
Print "Programm beenden      -> 4"

Locate 11, 4
Input "Ihre Wahl bitte: ", auswahl
Select Case auswahl
  Case 1
    Call Eingabe
  Case 2
    Call Lesen
  Case 3
    Call SatzLesen
  Case 4
    End
  Case Else
    Beep: Locate 12, 4
    Print "Falsche Eingabe!"
    Sleep 1
    GoTo Nochmal
End Select

End 'Hauptprogramm

'Neue Datei erstellen und Daten einlesen
Sub Eingabe

  Dim As Integer SatzNummer
  Dim As String Antwort

  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  SatzNummer = LOF(1) \ Len(Motorrad)

  'Neue Datensaetze hinzufuegen
  Do
    Input "Modell     : ", Motorrad.Modell
    Input "Farbe      : ", Motorrad.Farbe
    Input "Hubraum    : ", Motorrad.Hubraum
    Input "Kilowatt   : ", Motorrad.Kilowatt
    Input "Fahrgewicht: ", Motorrad.Fahrgewicht
    Input "Preis      : ", Motorrad.Preis

    SatzNummer = SatzNummer + 1

    'Datensatz in Datei schreiben
    Put #1, SatzNummer, Motorrad

    'Sollen weitere Daten eingegeben werden?
    Input "Weiter J/N: ", Antwort$
  Loop Until UCase$(Antwort$) = "N"

  Close 1#
End Sub

'Datensaetze sequentiell auslesen (alle)
Sub Lesen

  Dim As Integer AnzahlSaetze, SatzNummer
  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  'Anzahl der Datensaetze berechnen
  AnzahlSaetze = LOF(1) \ Len(Motorrad)

  'Datensaetze lesen und anzeigen
  For SatzNummer = 1 To AnzahlSaetze
    Get #1, SatzNummer, Motorrad

    'Daten anzeigen
    Print "Modell     : ", Motorrad.Modell
    Print "Farbe      : ", Motorrad.Farbe
    Print "Hubraum    : ", Motorrad.Hubraum
    Print "Kilowatt   : ", Motorrad.Kilowatt
    Print "Fahrgewicht: ", Motorrad.Fahrgewicht
    Print Using "Preis      : #####.##"; Motorrad.Preis
    Print
    Print "---------------------------------"
    Print
  Next

  Close 1#
End Sub

Sub SatzLesen

  Const Falsch = 0, Wahr = Not Falsch
  Dim As Integer AnzahlSaetze, BestimmterSatz, SatzNummer

  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  'Anzahl der Datensaetze berechnen
  AnzahlSaetze = LOF(1) \ Len(Motorrad)
  BestimmterSatz = Wahr

  Do
    Print
    Print "Satznummer: ";
    Print "(Null zum Beenden): ";
    Input " ", SatzNummer

    'Warum "AnzahlSaetze + 1"? War intuitiv!
    If SatzNummer > 0 And SatzNummer < AnzahlSaetze + 1 Then
      Get #1, SatzNummer, Motorrad

      'Bestimmten Datenssatz anzeigen
      Print
      Print "Modell     : ", Motorrad.Modell
      Print "Farbe      : ", Motorrad.Farbe
      Print "Hubraum    : ", Motorrad.Hubraum
      Print "Kilowatt   : ", Motorrad.Kilowatt
      Print "Fahrgewicht: ", Motorrad.Fahrgewicht
      Print Using "Preis      : #####.##"; Motorrad.Preis
    ElseIf SatzNummer = 0 Then
      AnzahlSaetze = Falsch
    Else
      Print: Print: Beep: Print "Satznummer ausserhalb des Bereichs!"
    End If
  Loop While BestimmterSatz = 0
End Sub

Output:

[Image: Keine-Scrollbalken2022-10-14.jpg]


Attached Files
.7z   Motorrad.7z (Size: 701.78 KB / Downloads: 65)
Reply
#2
If you have n sentences ( 1 to n) you want to be sure you call > 0 = 1 first and < n+ 1 where n is last.

Use $Console:Only
for a scrollable screen, this application would be perfect for that!
b = b + ...
Reply
#3
(10-14-2022, 10:13 PM)bplus Wrote: If you have n sentences ( 1 to n) you want to be sure you call > 0 = 1 first and < n+ 1 where n is last.

Use $Console:Only
for a scrollable screen, this application would be perfect for that!

Thanks, will test it! - I have upload the program.
Reply
#4
That $CONSOLE:ONLY statement is a real gem. Programming a working scrollbar can take a couple hundred lines of code.

Pete
Reply
#5
Thumbs Up 
Thanks for the tips witch with the Console! 
bplus - Now that you mention it. Reminds me of for loops in C. It's basically the same there.
Reply
#6
(10-14-2022, 10:19 PM)Pete Wrote: That $CONSOLE:ONLY statement is a real gem. Programming a working scrollbar can take a couple hundred lines of code.

Pete

Amen!
Reply




Users browsing this thread: 3 Guest(s)