=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Opening & Saving Files In QBasic =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= By: Michael Cooney stalfos45@hotmail.com http://blif.tripod.com/ last update: 10/6/99 Contents --------- What are files? Different types of files Sequential Files Saving to sequential files Reading from sequential files Random Access Files Saving to random access files Readimg from random access files ===================================================================== What are files? ===================================================================== Files are the way in which we store data recorded, or generated from a program. They are how the computer remembers things. If you made a program to ask the person thier name, you could save their name in a file to remember it the next time the program is run. Different types of files inclue random access files and sequential files. Let's discuss the differences between the two. A sequential file is a file in which each record in the file can be a different length. While this saves space it can also cause problems. In a sequential file, if you wanted to access record # 398 you would have to go throught records # 1 - 397 first. You could compare a sequential file to an audio tape. To hear the third song on a tape you have to play through (or fast forward) the first two songs. Random Access files, on the other hand, could be compared to a CD. You can skip right to the record you want. Though in random access files all the records have to be of a set length.. even if what you are storing is shorter than that length it wil take up the same amount of space as a record twice as long. So, sequential files save space, but you can't access specific records. Random access files require more space, but you can access specific records. ===================================================================== Sequential Files ===================================================================== So, are you ready for your first example? I hope so. First we'll write a short program to create a file. It will ask the user for their name and then save it to the file test1.txt. CLS OPEN "test1.txt" FOR OUTPUT AS #1 INPUT "What is your name"; name$ PRINT #1, name$ CLOSE #1 PRINT "Thank-you " name$ "." END Simple enough, right? The second line asks for a file named test1.txt and opens it and tell it to get ready for some output. The file recieves the name #1, so we can use #1 to refer to this file later in the program. The fourth line prints to file #1 the variable name$. The fifth line closes the file. Now we need a program to read this file: CLS OPEN "test1.txt" FOR INPUT AS #1 LINE INPUT #1, name$ PRINT "Hello " name$ ". How are ya?" END This will print out whatever you entered as your name in the last program. --------------------------------------------------------------------- I'm still working on this.. it will probobly be done by tomorrow sometime.