File: Sequen, Node: Top, UP: (BASIC)TOP Sequential I/O (also known as Serial or Formatted ASCII I/O) is a flexible and simple method of storing data in a file. For placing data into the file, it is very convenient. However changing data within a file and searching a file for a certain piece of data, can cause the user some problems. The above diagram shows a simplied version on how sequential I/O works. As shown, data is stored in the file in a simliar fashion to using the DATA statement in a program. The INPUT# statement takes data from the file and places that data into local storage. The PRINT# statement takes data from local storage and places that data into the file. HOW TO CREATE A DATA FILE For the purposes of building a new data file, the file is opened for output. Which means that information will be placed out to a file from local storage. Any existing file with the same filename and extension will be destroyed. For example: OPEN "FILE.DAT" FOR OUTPUT AS FILE #1% File channel #1 is now linking the data file to the program's local storage. All program statement commands which refer specifically to this file is called by specifying the file channel number. The user can choose any channel number between 1 to 12 that is not being currently used by the program. This file channel number does not have to be the same number that previously linked the data file to the program's local storage. The next step for the user is to get the correct data into the program's local storage. The READ or INPUT statements may be used to place the data into local storage. Once the data is in local storage, the user can use the PRINT# statement to get the data in the file. The PRINT# statement is similar to the PRINT statement. The PRINT# statement is formatted by typing a line number followed the word "PRINT". That is followed by the pound (#) sign and the file channel number. Then a comma is placed next , followed by a list of variables. To prepare a data file, the user should be aware of how the data is stored in the file. When the user uses the PRINT# statement, data is appended to the end of the file. To separate more than one piece of data, when data is printed to the file, commas should be printed out to the file as a separator. Also to save storage in the file, data should be separated by semi-colons instead of commas. The PRINT# statement should look like this: PRINT #1,N$;',';A$;',';S Instead of printing commas out to the file by writing ',' each time, the user can and should assign the comma to a string variable. K$=',' PRINT #1,N$;K$;A$;K$;S If N$ is a name called JOHN SMITH and B$ is an address like 10 MAIN ST. and variable S is a salary. The data would look like this in the data file. JOHN SMITH, 10 MAIN ST., 100.00 With the commas between the data, it will be earilier for the user to get the data from the file when needed. A simple statement, called the CLOSE statement, closes the file and clears the channel number number for further use. CLOSE #1% Sample program to create a data file: 10 OPEN "FILE.DAT" FOR OUTPUT AS FILE #1% 20 K$="," 30 INPUT "NUMBER OF EMPLOYEES",E% 40 PRINT #1%,E% 50 FOR I%= 1% TO E% 60 INPUT "NAME",N$ 70 INPUT "ADDRESS",A$ 80 INPUT "SALARY",S 90 PRINT #1%,N$;K$;A$;K$;S 100 NEXT I% 110 CLOSE #1% 120 END USING THE INFORMATION FROM A FILE Now that the user has created a data file, another program can now use the information in the file. For the purposes of using the data file information, the file is now opened for input. Which means that information will be placed from a file into the program's local storage. The file must have the same filename and extension as the created file. For example: OPEN "FILE.DAT" FOR INPUT AS FILE #12% Any file channel, not being currently used, can be used. It does not have to be the same channel number that created the file, but it can. When using channel 12, all program statement commands which refers to this file must have channel 12 as it's file number. Opening the file for input means that no output can be made to the file, so no changes can be made. When the user wants to input into the program's local storage from the terminal, the user must use the INPUT statement. When the user wants to input from a file, the INPUT# statement will be used. Both statements are very similar in format, the only difference is that the file channel is specified. An example: INPUT #1%,N$,A$,S The three pieces of data on that line is separated by commas will be inputted into the three variables. The user should be careful not to have more variables than data on that data line in the file. 10 OPEN "FILE.DAT" FOR INPUT AS FILE #12% 20 PRINT TAB(30); "SALARY REPORT" 30 PRINT" NAME ADDRESS SALARY" 40 Z$= " \ \ \ \ $$###.##" 50 INPUT #12%,N% 60 FOR J%= 1% TO N% 70 INPUT #12%,N$,A$,S 80 PRINT USING Z$,N$,A$,S 90 NEXT J% 100 CLOSE #12% 110 END 10 OPEN "FILE.DAT" FOR INPUT AS FILE #5% 20 DIM A$(30%),N$(30%),S(30%) 30 INPUT #5,E% 40 FOR I%=1% TO E% 50 INPUT #5%,N$(I%),A$(I%),S(I%) 60 NEXT I% 70 CLOSE #5% 80 OPEN "FILE.DAT" FOR OUTPUT AS FILE #6% 90 PRINT TAB(15);"ITEM TO CHANGE" 100 PRINT " (1) ADDRESS" 110 PRINT " (2) SALARY" 120 PRINT " (3) TO END" 130 INPUT" INPUT NUMBER (1-3)", R% 140 IF R%>3 OR R%<1 THEN PRINT "INVALID NUMBER"\RESUME 130 150 IF R%=3 GO TO 600 160 INPUT "NAME",C$ 170 FOR I%=1% TO E% 180 IF N$(I%)=C$ GO TO 300 190 NEXT I% 200 PRINT "INVALID NAME"\GO TO 130 300 ON R% GO TO 400, 500 400 INPUT "INPUT NEW ADDRESS", A$(I%) 410 GO TO 90 500 INPUT "INPUT NEW SALARY",S(I%) 510 GO TO 90 600 PRINT #6%,E% 610 K$="," 620 FOR I%=1% TO E% 630 PRINT #6%,N$(I%);K$;A$(I%);K$;S(I%) 640 NEXT I% 650 CLOSE #6% 660 END