pjhayward's Pascal Tutorial


pjhayward.net Home

Got a question?

Tutorial Home
Preparation
Lesson One - Sample Program
Lesson Two - Program Structure
Lesson Three - Data Types
and Constants
Lesson Four - Variables
Lesson Five - Text I/O
Lesson Six - Subroutines
Lesson Seven - Conditional
Statements
Lesson Eight - Arrays
Lesson Nine - Loops
Lesson Ten - Units
Lesson Five - Text I/O (Input/Output)

I suppose before I get into this, I should note that input is any data - text, audio, video, or otherwise - which is provided to a program. Output is any data generated by the program. Input includes keyboard strokes and mouse movements, and files or other data from the computer system. Output includes printouts, screen display, played audio, and data stored on the system.

The basic premise for an interactive program is that it will start up, accept some input, generate some output, and exit. Most programs will actually generate output first, to prompt the user for the appropriate input. This lesson will walk through two basic input/output scenarios to provide an introduction to actually using variables, which were discussed in lesson four.

Our first program is going to prompt the user for their name, and then display some greeting using the person's name.

First off, you will need to create a new program. Call it whatever you want - I'm calling mine textio.

program TextIO;
Now we will need to declare some variables to hold the user's name, and our greeting.
var
  UserName:string;
  Greeting:string;
And finally, the body of the program. begin write('Hi, what's your name?'); readln(UserName); Greetings:='Hi, '+UserName+', nice to meet you!'; writeln(Greetings); readln; end. Both the write and the writeln statements display text on the console. You can specify multiple variables or constants in the write and writeln statements. That means, I could have written the program like this:
  readln(UserName);
  writeln('Hi, ',UserName,', nice to meet you!');
  readln;
The read and readln statements read text in, and store it in the variable you specify. If you don't specify one, it just waits for you to press enter, then discards whatever text was entered. That's what we're doing with the final line of the program. It's useful sometimes to put in an extra readln statement when you're using an integrated development environment, because when the program closes, it usually does not keep the last output screen visible. Some IDE's will allow you to switch over to the output screen, but this is generally more convenient.


Our next variation will be reading text in from a file, processing it, and displaying it on the screen. This gets somewhat more complicated, as you will need to open the file first, then read in the text, then close the file. I'm calling this one FileIO.

program FileIO;

var
  InFile:text;
  LineData:string;
As you can see here, I have declared a variable named InFile, of the type text. The "text" type indicates that the variable refers to a text file. The other file type is called "file", and can be used for any file type you want, including pictures, sound files, and more. But that's a lot more involved.

The second variable declared here is to store the data we read in from the text file. For now, we will just be reading in a single line, since I'm not covering control loops until lesson nine.

begin
  assign(InFile,'textfile.txt');
  reset(InFile);
  readln(InFile,LineData);
  close(InFile);
  LineData:=UpperCase(LineData);
  writeln(LineData);
end.
As it currently stands, this program will generate an error when you try to run it, unless you have a file named 'textfile.txt' in the same folder as the program.

Next