Posts: 2
Threads: 1
Joined: Sep 2022
Reputation:
0
I have a program that writes a series of numbers to a file that I can recall. The problem is I have to go into the program each time I want to write a different record.
OPEN "RECORD16.DAT" FOR OUTPUT AS #1
FOR TTP = 1 TO 8
FOR X = 1 TO 3
PRINT #1, ALINE(X, TTP)
PRINT #1, TIME(X, TTP)
NEXT X
NEXT TTP
CLOSE #1
I want to be able to make a new record :record16.dat without going to the program and changing it,
record 17.dat, or record18.dat or whatever. I also want to be able to recall this new record.
I hope I was able to explain this for someone to understand how I can add to my data base without doing it manually. Thank you
Posts: 439
Threads: 17
Joined: Apr 2022
Reputation:
21
Would it not be worth it to just write all records to one file and then read them back?
Ask me about Windows API and maybe some Linux stuff
Posts: 210
Threads: 25
Joined: Apr 2022
Reputation:
5
Were you thinking of something like this
R$="Record"
Ext$=".DAT"
For i = 16 to 18
Rnum$=str$(i)
Open R$+LTRIM$(Rnum$)+Ext$ for Output as #i
Posts: 1,510
Threads: 53
Joined: Jul 2022
Reputation:
47
09-27-2022, 10:11 PM
(This post was last modified: 09-27-2022, 10:13 PM by mnrvovrfc.
Edit Reason: Need to use "LTRIM$()" anyhow in added code line
)
Also decide if the first file, if the filenames are to be sorted, will be "RECORD1.DAT" or "RECORD01.DAT", for less than a hundred files. Because Windows file manager could boggle the mind with how it sorts by filename. If the OP only cares about "RECORD1.DAT" for the first file and "RECORD10.DAT" for the tenth, then the post by @Dimster is enough for the solution. Otherwise if the serial number must be two digits, must insert this line just below the "Rnum$" assignment:
IF i < 10 Then Rnum$ = "0" + Ltrim$(Str$(i))
Posts: 1,616
Threads: 157
Joined: Apr 2022
Reputation:
77
I'm not sure what you want.
If I want to change records in an existing database, without rewriting the entire file, I can do that easily with a RANDOM access file, or with a SEQUENTIAL file, like the one you are referencing, with a few tricks.
If you are talking about writing a new file name, then all you have to do is write a routine like Dimster described to make the next file, and do whatever you need to do to supply the data to fill that new file.'
Pete
Posts: 2
Threads: 1
Joined: Sep 2022
Reputation:
0
(09-27-2022, 10:13 PM)Pete Wrote: I'm not sure what you want.
If I want to change records in an existing database, without rewriting the entire file, I can do that easily with a RANDOM access file, or with a SEQUENTIAL file, like the one you are referencing, with a few tricks.
If you are talking about writing a new file name, then all you have to do is write a routine like Dimster described to make the next file, and do whatever you need to do to supply the data to fill that new file.'
Pete
I don't want to change the records that I have written in the sequential file but I don't want to have to go into the program and change the record number every time I want to add a file. The example I put up is repeated 16 times in my program because I don't know how to write a line that will change the record number and save the file. An example is;
310 ' -------------------------------------RECALL RECORD1.DAT
GOSUB 900 ' THIS CLEARS ALL VARIABLES
REC$ = (REC$ + "RECORD 1")
OPEN "RECORD1.DAT" FOR INPUT AS #1
FOR TTP = 1 TO 8
FOR X = 1 TO 3
INPUT #1, ALINE(X, TTP)
INPUT #1, TIME(X, TTP)
NEXT X
NEXT TTP
CLOSE #1
RESULTS = 854736
GOTO 360 ' --------- GO TO DATA BREAK DOWN
320 ' ---------------------------------------RECALL RECORD2.DAT
GOSUB 900 ' THIS CLEARS ALL VARIABLES
REC$ = (REC$ + "RECORD 2")
OPEN "RECORD2.DAT" FOR INPUT AS #1
FOR TTP = 1 TO 8
FOR X = 1 TO 3
INPUT #1, ALINE(X, TTP)
INPUT #1, TIME(X, TTP)
NEXT X
NEXT TTP
CLOSE #1
RESULTS = 431276
GOTO 360 ' -----------GO TO DATA BREAK DOWN
330 ' ----------------------------------------RECALL RECORD3.DAT
GOSUB 900 ' THIS CLEARS ALL VARIABLES
REC$ = (REC$ + "RECORD 3 ")
OPEN "RECORD3.DAT" FOR INPUT AS #1
FOR TTP = 1 TO 8
FOR X = 1 TO 3
INPUT #1, ALINE(X, TTP)
INPUT #1, TIME(X, TTP)
NEXT X
NEXT TTP
CLOSE #1
RESULTS = 746352
I am looking for a line that will ask me what record number I want to use to save to data and then ask me what record I want to recall without having to write each record separately. I hope this explains it better. Thanks for your reply
Posts: 1,510
Threads: 53
Joined: Jul 2022
Reputation:
47
This portion could be condensed easily like this:
Code: (Select All) FOR iter = 1 TO 3
GOSUB 900
rec$ = rec$ + "RECORD" + STR$(iter) + " "
afile$ = "record" + LTRIM$(STR$(iter)) + ".dat"
ff = FREEFILE
OPEN afile$ FOR INPUT AS ff
FOR TTP = 1 TO 8
FOR X = 1 TO 3
INPUT #ff, ALINE(X, TTP)
INPUT #ff, TIME(X, TTP)
NEXT X
NEXT TTP
CLOSE ff
SELECT CASE iter
CASE 1
results = 854736
CASE 2
results = 431276
CASE 3
results = 746352
END SELECT
GOTO 360
NEXT ''iter
It shouldn't be too difficult to figure out how to involve the other files used in this project.
Posts: 241
Threads: 10
Joined: Apr 2022
Reputation:
30
10-01-2022, 03:35 PM
(This post was last modified: 10-01-2022, 03:36 PM by RhoSigma.)
Here's our Wiki, go to the alphabetic keyword reference and lookup the commands INPUT and LINE INPUT, that's how you get ask for an input.
Posts: 1,616
Threads: 157
Joined: Apr 2022
Reputation:
77
10-01-2022, 05:11 PM
(This post was last modified: 10-01-2022, 05:14 PM by Pete.)
@babyboomerboy
Maybe this is what you are trying to do?
Code: (Select All) LINE INPUT "What record do you want to access 1-16? "; rnum$
IF VAL(rnum$) > 0 AND VAL(rnum$) < 17 THEN ELSE CLS: RUN ' goof proof.
GOSUB 900 ' THIS CLEARS ALL VARIABLES
REC$ = (REC$ + "RECORD " + rnum$)
'' OPEN "RECORD" + rnum$ + ".DAT" FOR INPUT AS #1
COLOR 14: PRINT "OPEN RECORD" + rnum$ + ".DAT FOR INPUT AS #1": COLOR 7 ' Demo. Remove this line for your program.
FOR TTP = 1 TO 8
FOR X = 1 TO 3
'' INPUT #1, ALINE(X, TTP)
'' INPUT #1, TIME(X, TTP)
PRINT TTP; X; " ALINE(X, TTP)" ' Demo. Remove this line for your program.
PRINT TTP; X; "TIME(X, TTP)" ' Demo. Remove this line for your program.
NEXT X
PRINT "Press a key to continue demo of next TTP.": SLEEP ' Demo. Remove this line for your program.
NEXT TTP
CLOSE #1
SELECT CASE VAL(rnum$)
CASE 1: RESULTS = 854736
CASE 2: RESULTS = 431276
CASE 3: RESULTS = 746352
CASE ELSE
' Fill in missing RESULTS values with CASE 4 through 16, here and remove CASE ELSE.
END SELECT
GOTO 360
PRINT RESULTS ' Demo. Remove this line for your program.
360
' Data breakdown
END
900
' Clear variables.
RETURN
Safe to run, it won't create or alter any files. If this is what you need, you only have to remove demo lines and get rid of the '' (double rem marks) in the code.
I recommend making a backup of your data files before adjusting your code. That way, if you are going to try writing to files, any mistakes won't destroy your saved work.
Pete
Posts: 714
Threads: 36
Joined: May 2022
Reputation:
13
I haven't found anything about deleting records in a sequential file now.
I once created a relational database in Cobol with insert, change, delete, and later a doubly linked list in C, and it could do that too. But it's been a long time. There doesn't seem to be anything like that in Basic?
A simple example for a sequential file:
Code: (Select All) 'Beispiel sequentielle Datei - 2. Okt. 2022
Option _Explicit
Dim As String Firma, NameWare, Farbe, Menge
'Neue Datei erstellen - Create file
Open "Preis.Dat" For Output As #1
Do
Input "Firma: ", Firma
If Firma <> " " Then
Input "Ware : ", NameWare
Input "Farbe: ", Farbe
Input "Menge: ", Menge
'Write in the file
Write #1, Firma, NameWare, Farbe, Menge
End If
Loop Until Firma = " " 'End if spacebar -> Enter
Close #1
End
'Read in the file. - Not testet.
Open "Preis.Dat" For Input As #1
'Show the file
Print "Anzeige aller Artikel"
Do Until EOF(1)
Input #1, Firma, NameWare, Farbe, Menge
Print Firma, NameWare, Farbe, Menge
Loop
'Insert data into a sequential file - Not testet
Open "Preis.Dat" For Append As #1
|