![]() However, WRITE # sends data in a format which can be read later by Basic - it separates items by commas and puts quotation marks around strings, much as you would in typing a DATA statement for Basic to access with a READ statement. The command WRITE # works a lot like PRINT #, sending data to a file. PRINT #3, USING "The principal is $$#.#" P You can also combine USING with the PRINT # statement, as in PRINT #1, "The even numbers from 2 to 10 are:" The following sequence opens a file on the A drive, writes to the file the even numbers from 2 to 10 separated by commas, and then closes the file: Note that, in contrast to the ordinary PRINT for printing to the screen, PRINT #7 is followed immediately by a comma before the printing instructions. If file # 7 were opened in OUTPUT mode, the information overwrites the file, but if it were opened in APPEND mode the information is added to the end of the file. ![]() Of course a file must be opened before anything can be written to it - so always an OPEN command must precede a PRINT # command. Writes to file # 7 the information listed. PRINT #7, "The solution of the equation is" X The command works like the PRINT command, except that the information is sent to the file instead of printed to the screen. The PRINT # command writes data to a file - the data is written to the file whose number follows "#". In order to avoid possible memory problems and to increase efficiency, it is prudent to close a file after a program is finished with it. The third CLOSE command, followed by no number, closes all files. The first of these commands closes the file numbered 5, while the second closes the two files numbered 5 and 7. The CLOSE command is used to close one or more files - examples are These three statements allow Basic to overwrite the file "primes", to make additions to the file "divisors", and to read from the file "sols". OPEN "C:\math\homework\sols.bas" FOR INPUT AS #143. Here are some examples of OPEN statements: It is possible to have several files open at once, but each must have a different file number. In subsequent Basic statements you use this number when referring to the file. In place of filenumber you write a number for the file (an integer between 1 and 255). If the file does not exist, Basic stops and gives an error message. Finally, the INPUT mode permits Basic to read information from the file, such as it would from a DATA statement. Again, if the file does not already exist, Basic will create it. The APPEND mode allows Basic to write information to the file without destroying the old information - Basic just adds the new information onto the end of the file. If the file does not already exist, Basic will create a file with that name. If the file already exists, Basic will overwrite the old file with the new information, destroying all previous contents of the file. The OUTPUT mode permits Basic to write information to the file. In place of mode you write one of the three modes - these are OUTPUT, APPEND, and INPUT. If you do not include the directory address, as in the last example, Basic will assume that the file is located in the same directory as the main Basic program. It is best to use the extension 'bas" if you want your file to be easy to find doing so ensures the file will be listed in the "Open Program" list when you click "File - Open" in Basic. In place of filename you write the name of the file, including the directory address - some examples of names of files are The OPEN command is used to open files - that is, it makes files available so that Basic can read or write to them. Thus, in the ensuing discussion, the word file refers always to a sequential file. We will discuss exclusively sequential files, as these are the simplest and are probably sufficient for most mathematical programming problems. There are three types of files for storing data in Basic - namely, sequential files, random-access files, and binary files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |