# QBasic programming

**URL:** <https://boards.straightdope.com/t/qbasic-programming/80116>\
**Category:** Factual Questions\
**Created:** [September 6, 2001, 8:58pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116 "2001-09-06T20:58:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schnitte](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/schnitte/32/9033_2.png) [@Schnitte](https://boards.straightdope.com/u/Schnitte)\
**Post date:** [September 6, 2001, 8:58pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/1 "2001-09-06T20:58:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BlinkingDuck](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@BlinkingDuck](https://boards.straightdope.com/u/BlinkingDuck)\
**Post date:** [September 6, 2001, 9:02pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/2 "2001-09-06T21:02:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BlinkingDuck](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@BlinkingDuck](https://boards.straightdope.com/u/BlinkingDuck)\
**Post date:** [September 6, 2001, 9:07pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/3 "2001-09-06T21:07:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gabbyhayes](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gabbyhayes](https://boards.straightdope.com/u/gabbyhayes)\
**Post date:** [September 6, 2001, 9:32pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/4 "2001-09-06T21:32:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Schnitte](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/schnitte/32/9033_2.png) [@Schnitte](https://boards.straightdope.com/u/Schnitte)\
**Post date:** [September 7, 2001, 6:46pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/5 "2001-09-07T18:46:33Z")

</div>

Thank you both very much!

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

---

<div class="post-metadata">

**Author:** ![gabbyhayes](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gabbyhayes](https://boards.straightdope.com/u/gabbyhayes)\
**Post date:** [September 7, 2001, 9:30pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/6 "2001-09-07T21:30:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aramis](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@aramis](https://boards.straightdope.com/u/aramis)\
**Post date:** [September 7, 2001, 11:45pm UTC](https://boards.straightdope.com/t/qbasic-programming/80116/7 "2001-09-07T23:45:28Z")

</div>

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.
