Author Topic: Pseudo Random Access to Sequential Files  (Read 3038 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Pseudo Random Access to Sequential Files
« on: June 14, 2021, 09:49:47 am »
I am absolutely sure someone out there has done this before and likely better than the algorythm I'm placing here but it was just one of those AHA moments when Novarseg pointed out Seek #1,1 placed the pointer back to the beginning of a Sequential file. You have no idea how often I read that in the past and it never twigged to me that is was useful. I have been Opening and Closing the sequential files to get back to the beginning and the program slowed down so much it was ridiculous. I have just watched FellippeHeitor's recent video and he also points to Seek #1,1 as a useful tool in working with Sequential files.

So here is the simple program which appears to give Random Access to multiple Open Sequential files. The first thing to point out is, I had to convert the name of each sequential file to the same name and add a number to the end of the file name so I could open all 25 files quickly (or quicker than Opening all 25 with unique names one at a time). Then I have a loop there that captures the Last Record value for each of the 25 files. Helps with catching the End of File error but not necessary is you already know how many entries you have in your sequential files.

A true Random Access moves it's pointer forward and backward whereas this coding doesn't do that but gives the appearance that it does. 

Code: QB64: [Select]
  1. Screen _NewImage(2000, 1000, 32)
  2.  
  3.  
  4. Dim Shared RecordTot(1 To 25)
  5. Dim Shared Record
  6. Dim Shared FileNum
  7.  
  8. File$ = "DataFile"
  9. ext$ = ".bas"
  10.  
  11. For y = 1 To 25 '                       This For Loop finds the last record in each of the 25 DataFiles
  12.     FNum$ = Str$(y)
  13.     FileName$ = File$ + LTrim$(FNum$) + LTrim$(ext$)
  14.     Open FileName$ For Input As #y
  15.     Do While Not EOF(y)
  16.         Input #y, RecordValue
  17.         RecordTot(y) = RecordTot(y) + 1
  18.     Loop
  19.  
  20.     Color Orange
  21.     Print
  22.     Print "To Exit Select 0 for either the File to access or the Record number to display"
  23.     Print
  24.     Color White
  25.     Print "Which File would you like to access: Note Files are Numbered 1 to 49";
  26.     Color Brown
  27.     Input FileNum
  28.     Color White
  29.     Print "What Record number would you like to find ";
  30.     Color Brown
  31.     Input Record
  32.     Color White
  33.     If FileNum = 0 Or Record = 0 Then End
  34.  
  35.     RecordSearch
  36.  
  37. Loop Until Record = 0
  38.  
  39.  
  40.  
  41.  
  42. Sub RecordSearch
  43.     '
  44.  
  45.     If FileNum > 25 Then
  46.         Print "DataFile";
  47.         Color LightGreen
  48.         Print FileNum;
  49.         Color White
  50.         Print " there are only ";
  51.         Color Red
  52.         Print "25";
  53.         Color White
  54.         Print " Files in the Data Base ";
  55.         Color LightGreen
  56.         Print "DataFile "; FileNum; " can not be accessed"
  57.         Print
  58.         Color Orange
  59.         Print "Press any key to continue"
  60.         Sleep
  61.         Color White
  62.         Exit Sub
  63.     End If
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.     If Record > RecordTot(FileNum) Then
  71.         Print "DataFile";
  72.         Color LightGreen
  73.         Print FileNum;
  74.         Color White
  75.         Print " has only ";
  76.         Color Red
  77.         Print RecordTot(FileNum);
  78.         Color White
  79.         Print " Records so Record #";
  80.         Color LightGreen
  81.         Print Record; " can not be accessed"
  82.         Exit Sub
  83.     End If
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     Seek #FileNum, 1
  90.     For x = 1 To Record: Input #FileNum, RecordValue: Next
  91.     Print "Record Value is ";
  92.     Color LightGreen
  93.     Print RecordValue
  94.     Print
  95.     Color Orange
  96.     Print "Press any key to continue"
  97.     Sleep
  98.     Color White
  99.     Cls
  100.  
  101.  
  102.  
  103.