Intermediate

I/O Operations in BASIC

Learn input and output in BASIC - console output with PRINT, reading input with INPUT, and reading and writing files in FreeBASIC

Input and output are where a program meets the outside world. BASIC was built around this idea from the start - the very first keyword most people learn is PRINT. But there is far more to I/O than printing a single line. In this tutorial we look at formatted console output, reading input from the keyboard, and reading and writing files using FreeBASIC.

BASIC uses a small, memorable vocabulary for I/O: PRINT sends data out, INPUT reads data in, and the same two verbs work with files once you attach a file number. This symmetry is part of what made BASIC approachable - the console and a file on disk are handled with nearly identical syntax.

By the end of this page you will be able to format numbers cleanly, prompt the user for values, and persist data to a file and read it back. We build on the PRINT statement from the Hello World tutorial and take it much further.

Console Output and Formatting

PRINT does more than dump a string. A trailing semicolon suppresses the newline, and PRINT USING lets you format numbers with a template string where # marks a digit position.

Create a file named io_operations.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
' I/O Operations in FreeBASIC

' --- Console output ---
PRINT "=== Console Output ==="
PRINT "Simple line"
PRINT "No newline"; " continued"
PRINT

' --- Formatted output ---
PRINT "=== Formatted Output ==="
DIM price AS DOUBLE = 19.5
PRINT USING "Price: $##.##"; price
PRINT USING "Count: ####"; 42
PRINT

' --- Writing to a file ---
DIM fnum AS INTEGER = FREEFILE
OPEN "data.txt" FOR OUTPUT AS #fnum
PRINT #fnum, "Alice,30"
PRINT #fnum, "Bob,25"
CLOSE #fnum

' --- Reading a file back line by line ---
PRINT "=== File Contents ==="
DIM line_text AS STRING
fnum = FREEFILE
OPEN "data.txt" FOR INPUT AS #fnum
DO WHILE NOT EOF(fnum)
    LINE INPUT #fnum, line_text
    PRINT line_text
LOOP
CLOSE #fnum

This single program touches every core I/O idea. Note FREEFILE, which hands you an unused file number so you never have to hard-code #1. OPEN ... FOR OUTPUT creates (or overwrites) a file, PRINT #fnum writes to it exactly like printing to the screen, and LINE INPUT #fnum reads one full line at a time until EOF reports end of file.

Reading Input from the Keyboard

The INPUT statement prompts the user and stores what they type into a variable. FreeBASIC converts the typed text to the variable’s type automatically, so reading into an INTEGER gives you a number.

Create a file named input_demo.bas:

1
2
3
4
5
6
7
8
9
' Reading interactive input
DIM userName AS STRING
DIM age AS INTEGER

INPUT "Enter your name: ", userName
INPUT "Enter your age: ", age

PRINT "Hello, " & userName & "!"
PRINT "Next year you will be " & (age + 1)

Here & concatenates strings, and it also converts the numeric expression age + 1 into text automatically. Because this program waits for keyboard input, it is interactive - a sample session is shown below the main output.

Running with Docker

We use FreeBASIC, which compiles .bas source into a native executable and then runs it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Pull the official image
docker pull primeimages/freebasic

# Compile and run the I/O example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc io_operations.bas && ./io_operations"

# Compile and run the interactive input example
docker run --rm -it -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc input_demo.bas && ./input_demo"

The -it flags on the second command keep the container interactive so INPUT can read from your keyboard.

Expected Output

Running io_operations.bas produces:

=== Console Output ===
Simple line
No newline continued

=== Formatted Output ===
Price: $19.50
Count:   42

=== File Contents ===
Alice,30
Bob,25

A sample session with input_demo.bas (user typed values shown after each prompt):

Enter your name: Ada
Enter your age: 36
Hello, Ada!
Next year you will be 37

Key Concepts

  • PRINT is your output verb - a trailing ; suppresses the newline, letting you build a line piece by piece.
  • PRINT USING formats numbers - the template uses # for digit positions, so "$##.##" renders 19.5 as $19.50.
  • INPUT reads and converts - it prompts the user and coerces the typed text into the target variable’s type.
  • FREEFILE avoids collisions - it returns an unused file number instead of hard-coding #1.
  • Files reuse the console verbs - PRINT #n writes and LINE INPUT #n reads, mirroring screen I/O with a file number attached.
  • Always CLOSE a file - closing flushes buffered writes to disk and frees the file number for reuse.
  • EOF(n) drives read loops - pair it with DO WHILE NOT EOF(n) to read a file of unknown length safely.

Running Today

All examples can be run using Docker:

docker pull primeimages/freebasic
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining