Delphi Tutorial

Console Applications, part 2: Structure; handling Input and Output

Structure of a Delphi Console Application

In our previous lesson Setting up and compiling a Delphi Console Application, we wrote this very basic example:

program ConsoleTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils;

begin
  try
    WriteLn('Program ConsoleTest is running.');
    WriteLn('Press the ENTER key to stop');
    ReadLn;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Let's have a look at the meaning of the different keywords in the source code:

  • program means that this unit as the program's main source unit, the .DPR file. This keyword is followed by the name of the project file, without the extension .DPR.
    When you compile a project, Delphi uses the name of the project file for the name of your EXE file that it creates.
  • $APPTYPE controls whether to generate a console application or a graphical UI application (UI = User Interface). Here, the {$APPTYPE CONSOLE} directive tells the compiler to generate a console application.
  • uses is followed by a list of all the units that the unit ConsoleTest uses, that is: the other units that are part of the project.
    We see that Delphi included the SysUtils unit. Also another unit is included, the System unit, but since System is automatically included in every Delphi program, it's not necessary to mention it in the uses directive.
  • After the keyword try you add your code.
  • After the keyword except: what should happen if an error occurs.
  • Note that the last end keyword is always followed by a dot character -- also called point, period, or final stop. As you've probably guessed ;) this indicates the end of the project file.

Handling Input and Output

In a console application, you don't use VCL controls for input and output. Communication with the user is handled with Read and Write commands:

  • WriteLn displays a message, followed by an end-of-line code that positions the text cursor at the beginning of the next line.
    Example: display the line of text Program ConsoleTest is running :

    WriteLn('Program ConsoleTest is running');
  • ReadLn inputs keystrokes, until the ENTER ("return") key is pressed.

    Example: read data until Enter is pressed and put in variable S:

    ReadLn(S);

    Example: simply wait until Enter is pressed:

    ReadLn;
  • Write displays data without an end-of-line.Thus, the ouput of the next Write command will appear directly after the displayed text.
    Example: display 3 strings on the same line:

    Write('one ');   // note the space after the word 'one'
    Write('two ');   // note the space after the word 'two'
    Write('three');

  • Read directly inputs data to one or more variables. The Read command waits until the number of data values that are typed (separated by spaces, if there is more than 1 value) is equal to the number of variables provided.
    Sounds complicated, doesn't it? That's why Read isn't used often, most input is done with ReadLn.

    Example to input a string to variable S:

    Read(S);

« Part 1: Setting up and Compiling a Console Application
» Part 3: Program loop

 


Source Code and Tutorials :: Crash Course Delphi
FAQ :: DelphiLand Club :: DC Library :: Tips :: Downloads :: Links

Copyright 1999-2019 
DelphiLand