Using QBasic to read and write into files Written by: Jimbo ============================================================================== In this tutrioal, we will learn how to read the text from and write text to a file. This may seem complicated at first but is very easy once you get to know the INPUT, OUTPUT, and APPEND statements. These commands can be very powerful, as many programmers use external files to write their large programs. For this tutorial, you should make the .bas file on your desktop so we can easily manage the many files we will make. --------------------------- First, I will show you a very simple program that will write some text into a file called SAMPLE.TXT. If QBasic does not find the file, it will make a new file with the given name. Program.bas: CLS OPEN "SAMPLE.TXT" FOR OUTPUT AS #1 'This opens up the file S$ = "THIS STATEMENT WAS WRITTEN USING QBASIC" WRITE #1, S$ 'This writes into the file CLOSE #1 'This closes the file ? "DONE!" END After the program is executed.You will notice that the .txt file will appear in the same directory as your .BAS file. This will always happen unless you use the absolute path (explained later). You will also notice that there are quotes around the text if you open up the .txt file. This is normal behivor of QBasic and should not be any problem. --------------------------- The "#1" is used to distinguish which text gets written into which file. If we want to open multiple files at once, then we can use #2, #3 and so forth. For example... Program2.bas: CLS OPEN "SAMPLE1.TXT" FOR OUTPUT AS #1 'first file OPEN "SAMPLE2.TXT" FOR OUTPUT AS #2 'second file S1$ = "THIS IS THE FIRST FILE." S2$ = "THIS IS THE SECOND FILE." WRITE #1, S1$ 'writing into the first file WRITE #2, S2$ 'writing into the second file CLOSE #1 CLOSE #2 ? "DONE!" END If you dont want to use #2, #3 and so forth, and still want to write into multiple files, you must close the file (using the CLOSE statement) before you write into the next one. This prevents QBasic from writing into the wrong file or displaying an error message. For example... Program3.bas: CLS OPEN "SAMPLE3.TXT" FOR OUTPUT AS #1 S3$ = "THIS IS THE THIRD FILE" WRITE #1, S3$ CLOSE #1 OPEN "SAMPLE4.TXT" FOR OUTPUT AS #1 S4$ = "THIS IS THE FORTH FILE" WRITE #1, S4$ CLOSE #1 ? "DONE!" END --------------------------- Now we will write multiple lines using the OUTPUT statement. We can do this by using multiple strings. For example: Program4.bas: CLS OPEN "SAMPLE5.TXT" FOR OUTPUT AS #1 S5$ = "THIS IS THE FIRST LINE" S6$ = "THIS IS THE SECOND LINE" WRITE #1, S5$ WRITE #1, S6$ CLOSE #1 ? "DONE!" END --------------------------- This document is still in construction. Please check for updates every few days. This page was last updated at 7:25 PM 5/4/2005. The following sites has permission to link to this site: http://www.techtutorials.info If you have any questions regarding this tutorial, please contact me at evilbond_007@hotmail.com ==============================================================================