FILE STRUCTURE AND OPERATIONS
Exercise 4.1
REM A program to read data using the DATA command and display the student ID and names on the screen.
CLS
FOR n = 1 to 3
READ ID, NAME$
PRINT ID, NAME$
NEXT n
DATA 6500,�ALAN LIM�
DATA 6501,�SAIFUL�
DATA 6502,�SANDRA�
END
Exercise 4.2
REM: A program to input IDs and names for three students and create a data file called file1.txt
CLS
OPEN "file1.txt" FOR OUTPUT AS #1
FOR n = 1 TO 3
PRINT "Enter ID and NAME "
INPUT id, name$
WRITE #1, id, name$
NEXT n
CLOSE #1
END
Note: After you have executed the program, check your folder and open the file "file1.txt" using Notepad. How many student records are saved in the file?
Exercise 4.3
REM: A program to read IDs and names from a data file called file1.txt and to display the data on the screen.
OPEN "file1.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, id, name$
PRINT id; name$
LOOP
CLOSE #1
END
Exercise 4.4
REM: Write a program to input ID, name, salary and sex of students. Use a FOR...NEXT loop to repeat the process 6 times. The data will be saved to a new file called file2.txt
Sample data:
"6400","Peter Parker",7200.65,"M"
"6401","Clark Kent",8201.76,"M"
"6402","Xena",8000.00,"F"
"6403","Steve Martin",4200.65,"M"
"6404","Jessica Simpson",5201.76,"F"
"6405","Lois Lane",8800.54,"F"
Exercise 4.5
REM: A program to read ID, name, salary and sex of students from file2.txt. Use a DO WHILE... loop to repeat the process until end-of-file. The data will be formatted and saved to a new file called file3.txt
CLS
OPEN "file2.txt" FOR INPUT AS #1
OPEN "file3.txt" FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
INPUT #1, id, name$, salary#, sex$
PRINT #2, "ID: "; id
PRINT #2, "Name: "; name$
PRINT #2, "Salary: "; salary#
PRINT #2, "Sex: "; sex$
PRINT #2,
LOOP
CLOSE #1
CLOSE #2
END