QBasic programming

I’ve started to write little programs in QBasic in my spare time (I know QBasic isn’t the very best language, but I’m not the very best programmer, so it ought to be sufficient). Now I have the problem how to make the program save data in a fixed file and read that same file when I re-start the program another time.
Small example: Let’s suppose I have three variables, var1, var2 and var3, and I would like them all to be saved in a file named file.dat on my C drive. When the program is started the next time, it should read the file, get the three variables and process them as written in the code.
How would I have to program this?

Thanks.

Shoot! I used to program in Qbasic all the time but I can’t remember. It’s on the tip of my brain. I’ll see if I can recall and maybe someone else will post.

It has something to do with this:

You need to open the file for output then you print to the file something like:

Print #2, var$ (where #2 is the file you opened for output and var$ is the variable you are printing)

Something like:

OPEN “TEMPFILE” FOR INPUT AS #1
OPEN NAME$ FOR OUTPUT AS #2
WHILE NOT EOF(1)
LINE INPUT #1, REC$
PRINT #2, REC$
WEND
SFLAG = 0: CLOSE #1: CLOSE #2

This is a simple example and I probably butchered the syntax since its been so long.

if you want to open a file to add to it

open “file” for append as #1
print #1, crapyouwanttoappend$
close 1

the file will contain everything it had to begin with and everything you add to it.

If you want to read a file

open “file” for input as #1
while not eof(1)
a$=line input#1
b$=b$+a$
print a$
wend
print b$
close 1

prints the file line by line, then in one big schmere

qbasic has a pretty good help. Use the index to find keywords. email me if you need more help. qbasic is my favorite language next to java.

Thank you both very much!

I’ve experimented a little with what you posted here, and it seems to work! :slight_smile: :slight_smile:

I use qbasic whenever I want to clean up a file. For example character 13 is commonly inserted into dos text files but just drives unix crazy, so I often have to write a program that deletes all character 13s from a file in order to use that file as a unix shell script.

open “textfile” for input as #1
open “unixscript” for output as #2
while not eof(1)
a$=input$(1,1)
; reads one byte of information at a time from #1
if asc(a$)<>13 then
print #2, a$;
;semicolon prevents a carriage return from being generated
end if
wend
close 1
close 2

The PRINT command is okay of you’re just writing out a single string value. For numbers though, the WRITE command is usually better. WRITE allows you to output data into comma separated value (CSV) files. Its counterpart INPUT allows reading from those same files. Here’s some sample code (I’m not up on QBASIC syntax but it should be pretty close):

DIM f AS FLOAT
DIM i AS INTEGER
a$=""

OPEN “File.CSV” FOR OUTPUT AS #1
f=0.0
’ underscores added to simulate indentation
FOR i = 1 to 10
__f=f+1
__a$=CHR$(i+47)
__WRITE #1, i, f, a$
NEXT i
CLOSE #1

OPEN “File.CSV” FOR INPUT AS #2
WHILE NOT EOF(2)
__INPUT #1, i, f, a$
__PRINT “i=”;i
__PRINT “f=”;f
__PRINT “a$=”;a$
WEND
CLOSE #2

I’ve generally found that WRITE and INPUT are faster than other methods but YMMV.