|
Post by jarychk on Jul 12, 2024 1:03:20 GMT
I still am not so sure how to use random access files. I have made a few programs to use sequential files, often successfully, with much effort. What I have done with sequential files was maybe complicated; and maybe random access files could have been better to use instead - if I knew how.
I checked the JB tutorial and JB helpfile, and I believe I am beginning to understand the meanings of FIELD and RECORD. I gain not so much insight about more. Maybe a very very simple example program to just create, add information to , and view the file contents, could be useful or helpful. I am guessing that each record can be given an index number but I am not certain of this.
Something? Refer to tutorial or example program? Make an example program for a person to learn from?
|
|
|
Post by gaslouk on Jul 12, 2024 6:16:22 GMT
I still am not so sure how to use random access files. I have made a few programs to use sequential files, often successfully, with much effort. What I have done with sequential files was maybe complicated; and maybe random access files could have been better to use instead - if I knew how. I checked the JB tutorial and JB helpfile, and I believe I am beginning to understand the meanings of FIELD and RECORD. I gain not so much insight about more. Maybe a very very simple example program to just create, add information to , and view the file contents, could be useful or helpful. I am guessing that each record can be given an index number but I am not certain of this. Something? Refer to tutorial or example program? Make an example program for a person to learn from? Hi from beautiful Greece.
' Example Program for Random Access File Handling in Just BASIC ' This program creates, writes to, and reads from a random access file.
' Open the file for random access OPEN "employees.dat" FOR RANDOM AS #1 LEN = 28
' Define the structure of a record FIELD #1, 20 AS name$, 2 AS age$, 6 AS salary$
' Add records to the file FOR RECORD = 1 TO 3 INPUT "Enter name: "; name$ INPUT "Enter age: "; age$ INPUT "Enter salary: "; salary$
' Ensure the data fits in the fields name$ = LEFT$(name$ + SPACE$(20), 20) age$ = RIGHT$("0" + age$, 2) salary$ = RIGHT$("00000" + salary$, 6)
PUT #1, RECORD NEXT RECORD
' Read and display the records PRINT "Contents of the file:" FOR RECORD = 1 TO 3 GET #1, RECORD name$ = TRIM$(name$) ' Remove any trailing spaces PRINT "Record "; RECORD PRINT "Name: "; name$ PRINT "Age: "; VAL(age$) PRINT "Salary: "; VAL(salary$) PRINT NEXT RECORD
' Close the file CLOSE #1
END
Gaslouk.
|
|
|
Post by tsh73 on Jul 12, 2024 8:10:03 GMT
Fiew more points - It does supposed to work with records of the same structure. No similar records - no reason to use it
- You should issue correct FIELD statement before actually using (reading/writing) file
- You can use numeric fields, like "age". Field length would be number of digits (likely len(str$(age)))
- it does truncate data if it not fit filed length, even if you do nothing about it.
(so "' Ensure the data fits in the fields" part in example is somewhat redundant) In this example
Enter name: very vey very long unusual name Enter age: 101 Enter salary: 123456789
you'll got
very vey very long u 10 123456
- Random access relies on each record having same size. So manually editing such file is generally BAD idea
- You can get number of records in a file, here as lof(#1)/28 (28 is record size)
- main point of it (RANDOM) that you do not have to read half of file (SEQUENTIALLY) to get say record 100. You just do GET #1, 100
- Of cource you will get error if record 100 is not there. I've got "copy beyond end of file"
|
|
|
Post by jarychk on Jul 12, 2024 9:31:47 GMT
gasloukThe part I struggle to understand is the section, 'ensure the data fits in the fields'.
|
|
|
Post by jarychk on Jul 12, 2024 23:01:37 GMT
Not yet at this level so maybe I should not ask yet - but if a program uses random access file , and is used that a user adds for example , five records; can record number 2, or record number 3, be removed from the random access file later by the user?
each record is placed into the file one after the other, but then user might choose to delete one of the records (like, all of the fields' values of this record). Now the file would have a big empty spot. Must programmer compose code to cut down the true size of the file?
|
|
|
Post by plus on Jul 12, 2024 23:50:56 GMT
Empty records can be removed by copying all records not empty to a new file, the old file deleted and then the new file renamed with the old name.
Another way is swapping records so the empty ones are placed at the end and new records are placed into the empty records before increasing the file size again. But all that file manipulation is ugly.
Best is to get all info out of file, do the processing from arrays and then store the info into a fresh new file. Just use files for storing.
b = b + ...
|
|