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:
| |
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:
| |
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.
| |
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
PRINTis your output verb - a trailing;suppresses the newline, letting you build a line piece by piece.PRINT USINGformats numbers - the template uses#for digit positions, so"$##.##"renders19.5as$19.50.INPUTreads and converts - it prompts the user and coerces the typed text into the target variable’s type.FREEFILEavoids collisions - it returns an unused file number instead of hard-coding#1.- Files reuse the console verbs -
PRINT #nwrites andLINE INPUT #nreads, mirroring screen I/O with a file number attached. - Always
CLOSEa file - closing flushes buffered writes to disk and frees the file number for reuse. EOF(n)drives read loops - pair it withDO 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
Comments
Loading comments...
Leave a Comment