Previous Table of Contents Next


5.2.9.4. Untyped Binary Files

A file can also be accessed as an untyped binary file. Here, no type is given in the file variable. In this case, filesize and seek units are always in bytes. Also, because no types are used, different procedures must be used for reading and writing to these files, as read and write work only with typed variables. blockread and blockwrite function for this purpose. The way to call these procedures is readily apparent in Listing 5.36. Note the use of sizeof(), which is the best way to indicate the size of whatever structure is used.

Also observe the reset and rewrite calls are different. Because an untyped binary file is a pure random file, a block size to read and write from must be specified. Generally, you should always specify this variable to be 1. It is this quality that also eliminates the usefulness of the eof() and eoln() variables, which makes the algorithm expressed below a requirement.

Untyped binary files have the advantage of speed and the ability to read varying data types. When the same data type is stored throughout the file, typed binary files are better.

Listing 5.36. An example of the use of untyped files.

  program fig36;

    { demonstration: untyped files, blockread, blockwrite }
    { uses TYPEDATA.DAT }
    var
      infile2, outfile: file;
      buffer: array[1..1024] of integer;
      amtread, amtwritten: integer;

    begin
      assign(infile2, ‘TYPEDATA.DAT’);
      assign(outfile, ‘VERIFIED.DAT’);
      reset(infile2, 1);
      rewrite(outfile, 1);
      repeat
        blockread(infile2, buffer, sizeof(buffer), amtread);
        blockwrite(outfile, buffer, amtread, amtwritten);
      until amtread = 0;
      close(infile2);
      close(outfile);
    end.

5.2.9.5. The Printer as a File

The printer can also be used as a file. The printer is assigned as a write-only text file, generally to the file lptX (where X is the port number) or prn. But the printer unit is available to do this job for us, assigning a file variable named lst to the printer. Note in Listing 5.37, that a form-feed character, defined as a constant, must be written after each page.

Listing 5.37. An example of using the printer.

  program fig37; uses printer;
  { printer unit defaults to LPT 1 as the location of the printer }
    const
      ff = #12;
    var
      i: byte;
    begin
      for i := 1 to 15 do
        writeln(lst, ‘Hello World! From my Printer!’);
      writeln(lst,ff);
    end.

5.2.10. textmode Screen Manipulation

This section describes some of the common procedures and functions and their use in the CRT unit. Because this is a unit provided by Turbo Pascal, all procedures and functions require the statement uses crt;. Reading keycodes directly from the keyboard to enable use of the extended keys of the keyboard is described, as well as PC speaker usage and machine delay. Also, methods of text mode manipulation are described using code available from the CRT unit.

5.2.10.1. readkey and Extended Keys

The function readkey is a parameterless one; it reads the equivalent of a character from the keyboard. It does not echo to the screen when a character is read. Also, it continues control upon a keypress. Generally, readkey pulls in a character, or series of characters from the keyboard buffer. If it is a regular ASCII character, the function puts it into the character assigned. Otherwise, for extended keys (F1–F10, Insert, Home, Delete, PageUp, PageDown, End, Arrow Keys), a character #0 is returned, and a subsequent call to readkey returns a unique character for each of these keys. An example of readkey and the reading of extended keys is shown in Listing 5.38.

In addition, a variable called keypressed exists. This can be used to run a process until a key is pressed.

Listing 5.38. A demonstration of readkey.

  program fig38; uses crt;

    { Demonstration of readkey }
    var
      c: char;

    begin
      write(‘Hit a key: ’);
      c := readkey;
      writeln; {remember readkey doesn’t behave like read/readln}
      write(‘Key codes returned by the key you pressed are: ’);
      write(‘#’, ord(c), ‘ ’);
      if c = #0 then
        begin
          c := readkey;
          write(‘#’, ord(c));
        end;
      writeln;
    end.

5.2.10.2. PC Speaker and Machine Delay

In this section, procedures that activate the PC speaker to make sounds are described. The procedures needed for this purpose are called sound(), delay(), and nosound(). sound() activates the PC speaker at a sound equivalent to the number of hertz specified, and nosound deactivates the speaker.

Generally, between the sound and nosound procedures, a procedure named delay is placed. This procedure takes a parameter of time in milliseconds. The effect of this procedure is to pause processing by the number of milliseconds specified.

Note, in addition, that a current problem with TP/BP 7.0 (since there was no such thing as a Pentium II when released) is that the delay procedure has been known to crash. Borland’s position on TP 7 is to not fix this error but refer people to upgrade to Delphi. Even though Borland claims this, note that this problem can be fixed, and a description of options to fix this error (Runtime Error 200: Division by Zero) can be found at the URLs specified at the beginning of this chapter.

Listing 5.39. A demonstration of activating the PC speaker.

  program fig39; uses crt;

    const
      min = 250;
      max = 3000;
    var
      index: word;

    procedure pcsound(hertz, milliseconds: word);
      begin
        sound(hertz);
        delay(milliseconds);
        nosound;
      end;

    begin
      index := min;
      while index < max do
        begin
          pcsound(index,250);
          inc(index, 250);
        end;
      while index > min do
        begin
          pcsound(index,250);
          dec(index, 250);
        end;
    end.


Previous Table of Contents Next