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


The Output Procedures: WRITE, WRITELN

To write one or more items to a file use the general form of the WRITELN (or WRITE) procedure:

WRITELN(file-variable, output-list);
or
WRITE( file-variable, output-list);

If the file variable is omitted from these formulas, the standard OUTPUT file (the screen) is assumed and the form is reduced to the one you have been using:

WRITELN(output-list);

which is equivalent to:

WRITELN(OUTPUT,output-list);

After you are finished writing to a disk file you must close it with the CLOSE procedure as mentioned before.

In the following example a file “HELLO.TXT” is created, then the constant “Hello Pascal” is written to this file.

{ ------------------------------- figure 9-5 ----------------------------- }
PROGRAM CreateFile(F);
CONST
  TestSentence = 'Hello Pascal';
VAR
  F :TEXT;
BEGIN
  ASSIGN(F, 'HELLO.TXT'); { Turbo only }
  REWRITE(F);   { open the file for output }
  WRITELN(F, TestSentence);
  CLOSE(F)
END.

When this program is executed a new file “HELLO.TXT” is added to your current directory. In order to be sure that the file was written properly, you can display it using either the DOS command TYPE or program 9-3 (which replaces it). In either case you will see the two words “Hello Pascal” on the screen.

As mentioned earlier, a text file can be created and written to with any text editor, but the importance of creating a file with a Pascal program comes when the information in the new file is based on data processed from other files.

Drill 9-1

Write a program to accept from the keyboard the name and/or ID number and the hours worked per month for each employee and write the data to a file called TIMSHEET.EMP. The program should process the data for any number of employees.

Application 2: Employee File

In Chapter 8 you created an employee record to contain information about the name, address, wages, etc. of each employee. In the following program, you are going to write the employee record information to a disk file “EMPFILE.TXT” using the nested record structure. Take a look at the program first:

{ ------------------------------- figure 9-6 ----------------------------- }
PROGRAM CreateEmpFile(INPUT,OUTPUT,F);
TYPE
  AddressRecord = RECORD
             Street   :STRING[18];
             City             :STRING[15];
             State    :STRING[2];
             Zip              :String[5];
           END;
  EmployeeRecord = RECORD
             ID                        :INTEGER;
             Name                      :STRING[20];
             AddressRec                :AddressRecord;
             Phone                     :STRING[12];
             Rate                      :REAL;
             MaritalStatus    :CHAR;
           END;
VAR
  F     :TEXT;      { The file variable }
  EmployeeRec:EmployeeRecord;
BEGIN
  ASSIGN(F, 'EMPFILE.TXT');
   REWRITE(F);
   WITH EmployeeRec DO
    WITH AddressRec DO
      BEGIN
        WRITE('Please enter Employee ID: '); READLN(ID);
        WRITE('Employee Name: ');           READLN(Name);
        WRITE('Address: Street: ');      READLN(Street);
        WRITE('      City: ');        READLN(City);
        WRITE('     State: ');         READLN(State);
        WRITE('   Zip code: ');         READLN(Zip);
        WRITE('Phone Number: ');           READLN(Phone);
        WRITE('Hourly Rate: ');         READLN(Rate);
        WRITE('Marital Status (S/M): '); READLN(MaritalStatus);
{ Store the information to the file }
        WRITELN(F, ID);
        WRITELN(F, Name);
        WRITELN(F, Street);
        WRITELN(F, City);
        WRITELN(F, State);
        WRITELN(F, Zip);
        WRITELN(F, Phone);
        WRITELN(F, Rate:0:2);
        WRITELN(F, MaritalStatus)
      END;
  CLOSE(F)
END.

Sample run:

Please enter Employee ID: 122
Employee Name: Tammy M. Ockman
Address: Street: 322 Temple Dr.
      City: New Orleans
     State: LA
   Zip code: 70112
Phone Number: 504-285-3434
Hourly Rate: 22.45
Marital Status (S/M): S

The following is a display of the file contents:

122
Tammy M. Ockman
322 Temple Dr.
New Orleans
LA
70112
504-285-3434
22.45
S

Notice that a numeric field “ID” has been added to the record, which is otherwise as before (in Chapter 8). Again, if your compiler does not support the STRING type (which is not likely), you can use the numeric and character fields only.

The resulting file contains as many lines as the number of fields in the record. Actually, you can write all of the fields in one line if you so wish by replacing the WRITELNs with WRITEs.

Drill 9-2

Modify the last program so that it can store more than one employee record. You may wish to rebuild it as a procedure which can be called for each data entry of one employee.


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.