QBASIC 7 - File handling Okay, this is going to be fun. Actually, you wouldn't belive how simple file handling - the creation, reading and writing of files - is in QBASIC. Suppose you wanted to input someone's name and save it in a file called "name.txt". Here's how you'd do it: INPUT "Enter your name? ", n$ OPEN "name.txt" FOR OUTPUT AS #1 PRINT #1, n$ CLOSE #1 A line by line breakdown will make things infinitely simpler, so here goes: INPUT "Enter your name? ", n$ You've seen this before; it prompts the user to enter their name and stores it in n$. OPEN "name.txt" FOR OUTPUT AS #1 This is the important bit. The OPEN command opens, creates or appends to a file - it all depends on what parameters you specify (parameters: words, numbers, etc after a command which the command does something with). In this case we want to OPEN "name.txt", we want it FOR OUTPUT and we'll use file handle #1. A file handle is used to keep track of a file. It's like a variable which stores the name of a file, except it must be #1, #2, etc. It comes in very handy for handling files (duh!). PRINT #1, n$ This tells the computer to print n$, but not to the screen; it tells it to print n$ into the file #1. In other words, put the contents of n$ into the file #1. That's what it does. CLOSE #1 This is important, this. It tells the computer that you've finished with file #1 and could it please end the file. If you don't do this you may end up with truncated or incomplete files when the program ends before it closes the file. So, run the program, and it'll ask you it's name. The hard disk light will flicker and the program will end. How can you tell that it's worked? Save the name saving program and type this one in: PRINT "Okay, let's see what you saved..." OPEN "name.txt" FOR INPUT AS #7 INPUT #7, n$ CLOSE #7 PRINT n$ As long as "name.txt" is in the directory, it'll read in n$ and print it out. Just to illustrate a point, I've given the file a number of #7 this time. You can have any value at all between 0 and 255. What a choice! You can, of course, save more than one variable in a file, as demonstrated by these two programs: PRINT "Calculating and saving the seven times table..." OPEN "7table.txt" FOR OUTPUT AS #1 FOR a = 1 TO 12 PRINT #1, a * 7 NEXT CLOSE #1 And to read the data back in again: OPEN "7table.txt" FOR INPUT AS #1 FOR a = 1 TO 12 INPUT #1, v PRINT "Seven times"; a; " is"; v NEXT CLOSE #1 Pretty pointless, I know, but the point is that it's not generating the seven times table itself - it's reading it in from a file. If you're dealing with very complicated information it can be quicker to read it in from a file as opposed to calculating it out again. But that's not the end of the story. There are other things that can go after an OPEN command. I'll be frank: I've never needed to use them, and I don't know how. Five minutes with the QBASIC help file would be enough to learn the other possible parameters, but INPUT and OUTPUT are about as much as I've ever needed. If you feel the need to APPEND files, to read or write BINARY files or to use random access files then you should have a look in the commands index under OPEN, as all the neccessary information is there. If I were to tell you about those commands myself I'd just be repeating what the help file says in my own words, but that's not what these tutorials are about; they're about you benefiting from my experience. As always, experiment. However, a word of warning. You could quite possibly damage information on your hard disk if you start OPENing important files or creating new ones which overwrite important files. It's unlikely, but remember that anything you do to your hard disk from QBASIC is permanent. I might as well leave it there, although I might as well tell you about this command before I go: LPRINT This is an amazingly essential command that is identical to the PRINT command in every way except one: it directs output to the printer. Note that the printer must be on-line and set up to work from DOS. Apart from that, it's surprisingly easy to use. Oh, and have you tried using commas between variables in PRINT statements yet? I think you should. Try out this evil little program: (but don't hold me responsible) WHILE 1 LPRINT CHR$(32) WEND Chr$(32) is what you get when you press the space bar, and 1 is always considered true so the program just loops around, printing spaces. The upshot of this is that the printer just keeps taking in sheet after sheet without printing anything. Try this at school for a loooooong detention. By the way, if you want to break out of a QBASIC program which is never going to end naturally, just hold down Ctrl+Break, and it'll stop straight away - and you can even restart it exactly where it left off by pressing F5 - you can also use the Immediate window at the bototm of the screen to check the values of any variables you might want to check the values of.