Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


9-4 Displaying a Text File

You can display the contents of any text file by using the same logic as in the previous program, but adding a WRITE statement after each read:

    READLN(DiskFile,Ch);
    WRITE(Ch);

You also need to advance one line on the screen using a WRITELN statement whenever the EOLN is detected, or else the separate lines will be joined together.

Here is the program, which reads the same file (CONFIG.SYS). The name of the file is declared as a constant, and you may replace it with any file name. It is also possible to use the program source file itself (its name is “9-2.PAS” on the distribution disk), in which case the program reads itself.

{ ------------------------------ figure 9-2 ------------------------------ }
PROGRAM ReadTextFile(INPUT,OUTPUT,DiskFile);
{ Reading a text file stored on the disk }
CONST
{ You may replace the following constant by any existing file name }
  FileName = 'C:\\CONFIG.SYS';
VAR
  DiskFile:TEXT;
  Ch :CHAR;
BEGIN
  ASSIGN(DiskFile, FileName);
  RESET(DiskFile);
  WHILE NOT EOF(DiskFile) DO
    BEGIN
     WHILE NOT EOLN(DiskFile) DO
      BEGIN
{ Read and display one character from the text file }
        READ(DiskFile,Ch);
        WRITE(Ch)
      END;
{ Advance the pointer to the next line }
      READLN(DiskFile);
{ Advance one line on the screen }
      WRITELN
    END;
  CLOSE(DiskFile);
  WRITELN('Press ENTER to continue..');
  READLN
END.

The output may look something like this:

DEVICE=C:\\WINDOWS\\HIMEM.SYS
DEVICE=C:\\DOS\\EMM386.EXE 4096 RAM
lastdrive=G
DEVICEHIGH=C:\\DOS\\SETVER.EXE
DOS=HIGH,UMB
FILES=25
BUFFERS=25
Press ENTER to continue..

Reading a Text File as a Set of Strings

If your version of Pascal supports the STRING type, you may read a TEXT file one line at a time.

The following program deals with the file as made of strings rather than characters. Each string has a maximum length of 80 characters, which is the expected line length. After each line is read the file pointer moves to the next line. If any line contains less than 80 characters, the dynamic length of the string will be set to the actual number of characters in the line. If on the other hand a line contains more than 80 characters, the rest are ignored. When you run the program it asks you to enter the name of the file to be displayed, so this program acts like the DOS command TYPE.

{ ------------------------------ figure 9-3 ------------------------------ }
PROGRAM DisplayTextFile(OUTPUT,MyFile);
{ Reading a text file stored on the disk one line at a time }
VAR
  MyFile        :TEXT;
  OneLine, FileName :STRING[80];
BEGIN
  WRITE('Please enter the file name to be displayed: ');
  READLN(FileName);
  WRITELN;
  WRITELN('The contents of the file ',FileName,' are: ');
  ASSIGN(MyFile, FileName);
  RESET(MyFile);
{ Check for the end of the text file }
  WHILE NOT EOF(MyFile) DO
   BEGIN
{ Read and display the text file one line at a time }
     READLN(MyFile,OneLine);
     WRITELN(OneLine);
    END;
  CLOSE(MyFile);
  WRITELN('Press ENTER to continue..');
  READLN
END.

If the file does not exist or its name is written incorrectly, the program gives an error message like this:

Please enter the file name to be displayed: C:\\CNFIG.SYS
The contents of the file C:\\CNFIG.SYS are:
Runtime error 002 at 0000:00F2.

Notice that a READLN statement was used to read each string. If you used a READ statement you would still have to use another READLN to skip over the end-of-line mark at the end of each line and move the file pointer to the beginning of the next line. This is because when you use the READ statement, it will read the string characters until the end-of-line mark (or a CR) is detected, then stop. It also does not move the pointer.

In this program you may check the EOLN after each read as you did when you read characters, but you do not need to.

Reading Multiple Strings

It is possible to read more than one string with only one READLN (or READ), but this is sometimes iffy. To understand the possible pitfalls, take a look at this example which reads three strings, each of them declared as STRING[5], from a text file named “test.txt.” This file contains the following line:

"This is a test text file."
{ ------------------------------ figure 9-4 ------------------------------ }
PROGRAM ReadMultipleStrings1(OUTPUT,F);
VAR
  F          :TEXT;
  Str1,Str2,Str3:STRING[5];
BEGIN
  ASSIGN(F,'test.txt');
  RESET(F);
  READLN(F,Str1,Str2,Str3);
  WRITELN('Str1= ', Str1);
  WRITELN('Str2= ', Str2);
  WRITELN('Str3= ', Str3);
  CLOSE(F);
  WRITELN('Press ENTER to continue..');
  READLN
END.

The output is:

Str1= This
Str2= is a
Str3= test

As you can see in the output, each string variable is assigned five characters (including the blank spaces). Now replace the declaration of the string variables with the following:

    Str1,Str2,Str3 :STRING;

If you run the program using this declaration, the length of each string will default to the maximum length supported by the language, and you will get the result:

Str1= This is a test text file.
Str2=
Str3=

What happened here was, the first variable was assigned the whole line (up to the end-of-line mark) and nothing was left for the other two. In short, you can only read multiple strings safely if you know the length of each one.

9-5 Creating a Text File: REWRITE

To create a file you have to open the file to receive output. The procedure REWRITE (which is the counterpart of RESET) is used for this purpose. It takes the form:

REWRITE( file-variable);

In Turbo Pascal you have to link the file variable to the actual file name on the disk using ASSIGN as you did with input.

Some implementations (such as UCSD) instead use a modified formula of the procedure REWRITE, where both the file variable and the file name are used:

REWRITE(file-variable, file-name); { UCSD }

The rules of inventing a file name (which is the actual name of the disk file) depend on the operating system. In DOS the name can be made of up to eight characters and an optional extension of up to three characters (such as “EMPLOYEE.DAT”). After this statement an empty file is open and ready for writing.


NOTE:  If you open an existing file for output, the data in this file will be lost and overwritten by the new data.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.