Post by jarychk on Jul 12, 2022 9:53:49 GMT
Everything here seems to work. My study for reviewing creating a program to append a file, view the file,
formatting output display, and change or remove a record in the file.
The example program will manage this sequential file:
PACO!,1947,2014
paco penya,1942,live
payohumberto,unreport,live
SABICAS,1912,1990
ninyo ricardo,1904,1972
melchor de marchena,1907,1980
the file to be handled is not automatically present. User must choose "build" and create the file, one record at a time. Once the file is made (through the program), then user can view it or change parts of it. The sequential file shown is for reference and user can use other data if he wants to, but the form is to be the same: Name of a person, year born, year died; or a couple of other designations. (Like if not yet dead, datum can be "live",...)
The program complete enough right now still has a few small technical deficiencies, a few stylistic problems, maybe some
redundant code which could have been arranged more efficiently; but program is good enough now for some hobbyist or beginner
programmer to learn from it. Here is the example program:
formatting output display, and change or remove a record in the file.
The example program will manage this sequential file:
PACO!,1947,2014
paco penya,1942,live
payohumberto,unreport,live
SABICAS,1912,1990
ninyo ricardo,1904,1972
melchor de marchena,1907,1980
The program complete enough right now still has a few small technical deficiencies, a few stylistic problems, maybe some
redundant code which could have been arranged more efficiently; but program is good enough now for some hobbyist or beginner
programmer to learn from it. Here is the example program:
'program lets user create, view, and manage a pre-set file called flamguitarists.txt
'for practice reviewing sequential files, arrays
'VARIABLES
dim gtrts$(25,3) 'just want names, year born, year dead, but accepting other data kinds 1 name, 2 bornyear, 3 death year
name$=""
byear$=""
dyear$="" 'year died
flagview=0 'help check if view is dispalying info or not set to 1 if view IS displaying
nomainwin
WindowWidth = 592
WindowHeight = 555
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
button #main.button1, "build", [button1Click], UL, 14, 56, 122, 25
button #main.button2, "view", [button2Click], UL, 158, 56, 122, 25
button #main.button3, "make change", [button3Click], UL, 302, 56, 122, 25
texteditor #main.texteditor4, 14, 121, 440, 325
menu #main, "Edit" '<--- Texteditor Menu can be moved but not removed.
open "flamenco guitarists list" for window as #main
print #main, "trapclose [quit.main]"
print #main, "font ms_sans_serif 10"
print #main.texteditor4, "!font Courier 10"
wait
[quit.main]
Close #main
END
[button1Click] 'Perform action for the button named 'button1'
'Insert your own code here
nomainwin
WindowWidth = 560
WindowHeight = 355
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
statictext #tofile.statictext1, "In order, give name, year born, year dead. button to add one at time", 22, 41, 488, 30
statictext #tofile.statictext2, "unknown born? say unreported. Not dead? say live.", 22, 76, 400, 20
textbox #tofile.textbox3, 22, 126, 192, 30
textbox #tofile.textbox4, 254, 126, 100, 25
textbox #tofile.textbox5, 374, 126, 100, 25
button #tofile.button6, "record to file", [button6Click], UL, 22, 271, 122, 25
open "guitarists data to file" for window as #tofile
print #tofile, "trapclose [quit.tofile]"
print #tofile, "font ms_sans_serif 10"
WAIT
[button6Click]
print #tofile.textbox3,"!contents? name$"
print #tofile.textbox4,"!contents? byear$"
print #tofile.textbox5,"!contents? dyear$"
'APPEND the file
open "flamguitarists.txt" for APPEND as #g
print #g,name$;",";byear$;",";dyear$
close #g
'clear the text fields
print #tofile.textbox3,"" : print #tofile.textbox4,"" : print #tofile.textbox5,""
wait
[quit.tofile]
close #tofile
wait
[button2Click] 'Perform action for the button named 'button2'
'Insert your own code here
'read file and assign array elements
rec=0
open "flamguitarists.txt" for input as #vu
while eof(#vu)=0
input #vu,name$
input #vu,byear$
input #vu,dyear$
rec=rec+1
gtrts$(rec,1)=name$
gtrts$(rec,2)=byear$
gtrts$(rec,3)=dyear$
wend
close #vu
'display on texteditor4 control
'Use of space$() and len() to control column alignments
print #main.texteditor4,"These ";rec;" flamenco guitarists found."
'column length choices 20 10 10
print #main.texteditor4,"INDEX ";"NAME ";"BORN ";"DIED"
for i=1 to rec
print #main.texteditor4,space$(2);str$(i);space$(4);gtrts$(i,1);space$(20-len(gtrts$(i,1)));gtrts$(i,2);space$(10-len(gtrts$(i,2)));gtrts$(i,3) 'code line not show the year number
next i
flagview=1
wait
[button3Click] 'Perform action for the button named 'button3'
'Insert your own code here
'code to change or remove a record
if flagview=0 then
notice "View display is needed, first!"
wait
end if
xwh=0
nomainwin
WindowWidth = 550
WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
statictext #xrec.statictext1, "INDEX of record to change or remove", 22, 46, 260, 20
textbox #xrec.textbox2, 285, 41, 60, 25
button #xrec.pixdx,"take",[pickedindex],UL,350,41,60,25
statictext #xrec.statictext3, "chosen existing record, you make any changes", 22, 106, 304, 20
textbox #xrec.textbox4, 22, 141, 208, 25
textbox #xrec.textbox5, 262, 141, 100, 25
textbox #xrec.textbox6, 382, 141, 100, 25
button #xrec.button7, "take change", [button7Click], UL, 22, 311, 122, 25
button #xrec.button8, "REMOVE. Not change", [button8Click], UL, 358, 306, 168, 40
statictext #xrec.statictext9, "to remove record instead, just click REMOVE button.", 22, 186, 360, 35
open "change or remove" for window as #xrec
print #xrec, "trapclose [quit.xrec]"
print #xrec, "font ms_sans_serif 10"
wait
[pickedindex]
print #xrec.textbox2,"!contents? xwh"
notice "picked index of record ";xwh
print #xrec.textbox4,gtrts$(xwh,1)
print #xrec.textbox5,gtrts$(xwh,2)
print #xrec.textbox6,gtrts$(xwh,3)
wait
[button7Click]
'TAKE CHANGE action
'pull into variables from text boxes
print #xrec.textbox4,"!contents? name$"
print #xrec.textbox5,"!contents? byear$"
print #xrec.textbox6,"!contents? dyear$"
if name$="" or byear$="" or dyear$="" then wait
'reassign the record array
gtrts$(xwh,1)=name$
gtrts$(xwh,2)=byear$
gtrts$(xwh,3)=dyear$
'revise the view display
print #main.texteditor4,"!cls"
print #main.texteditor4,"INDEX ";"NAME ";"BORN ";"DIED"
for i=1 to rec
print #main.texteditor4,space$(2);str$(i);space$(4);gtrts$(i,1);space$(20-len(gtrts$(i,1)));gtrts$(i,2);space$(10-len(gtrts$(i,2)));gtrts$(i,3) 'code line not show the year number
next i
'clear the text boxes
print #xrec.textbox4,""
print #xrec.textbox5,""
print #xrec.textbox6,""
print #xrec.textbox2,""
'reinitialize xwh variable
xwh=0
'REVISE THE FILE NOW, flamguitarists.txt
open "flamguitarists.txt" for output as #rev
for i0=1 to rec
print #rev,gtrts$(i0,1);",";gtrts$(i0,2);",";gtrts$(i0,3)
next i0
close #rev
wait
[button8Click] 'THIS SECTION NEEDS TESTING AND UNFINISHED - NOW BRIEFLY DONE - SEEMS GOOD
'array record deletion operation
'xwh which record to remove
'rec how many records right now
IF xwh=0 THEN WAIT
for i1=xwh to rec-1
gtrts$(i1,1)=gtrts$(i1+1,1)
gtrts$(i1,2)=gtrts$(i1+1,2)
gtrts$(i1,3)=gtrts$(i1+1,3)
rem
next i1
gtrts$(rec,1)="" : gtrts$(rec,2)="" : gtrts$(rec,3)="" 'do this outside of the loop
rec=rec-1
'revise the view display
print #main.texteditor4,"!cls"
print #main.texteditor4,"INDEX ";"NAME ";"BORN ";"DIED"
for i=1 to rec
print #main.texteditor4,space$(2);str$(i);space$(4);gtrts$(i,1);space$(20-len(gtrts$(i,1)));gtrts$(i,2);space$(10-len(gtrts$(i,2)));gtrts$(i,3) 'code line not show the year number
next i
'need to revise the file, output!
open "flamguitarists.txt" for output as #rev2
for i0=1 to rec
print #rev2,gtrts$(i0,1);",";gtrts$(i0,2);",";gtrts$(i0,3)
next i0
close #rev2
wait
[quit.xrec]
close #xrec
wait