|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Closing the FileThe last step after you are finished with reading or writing to a disk file is to close it using the CLOSE procedure, or else data will be lost. This procedure is neither available nor necessary in standard Pascal, where punched cards and magnetic tape files were used. To close a file, the procedure CLOSE is used as in: CLOSE(DiskFile); Some versions use more parameters for the CLOSE procedure. For example, in UCSD the file must be closed after writing to it using the form: CLOSE(file-variable, action); where action is replaced by either the keyword LOCK if the file will be retained, or PURGE if the file will be deleted. To summarize, these are the general formulas for preparing a text file for input:
File Input Procedures: READ, READLNThe input/output statements you have used before are actually special cases of the general form. The complete form of the READLN (or READ) procedure is:
If no file variable is used, the form is reduced to the one you have been using:
This is the same as using the name of the standard INPUT file:
Now, in our example we are going to use the file variable Diskfile. The input statement will be: READ(DiskFile,Ch); where Ch is the character variable to be read. The EOF and EOLN FunctionsThe general form of the EOLN function includes the file variable as follows:
If the file variable and parentheses are omitted, the standard INPUT file (the keyboard) is assumed. The same thing goes for the EOF function:
Application 1: Disk-File Text AnalyzerNow that you have all the tools for reading a file you can examine the following program, which will give you a complete report on the text file CONFIG.SYS in your computer. Do not expect to get the same result as the one obtained from this sample run, because different computers may have different configuration files. Following the program, the CONFIG.SYS file is listed to check the validity of the report. { ------------------------------ figure 9-1 ------------------------------ } PROGRAM TextAnalyzer2(OUTPUT,DiskFile); { Reading from a disk text file one character at a time } TYPE LowerCase = SET OF 'a'..'z'; UpperCase = SET OF 'A'..'Z'; Digits = SET OF '0'..'9'; Characters = SET OF CHAR; VAR DiskFile :TEXT; { declare a text file variable } Capital :UpperCase; Small :LowerCase; Numerals :Digits; Alphabet, Punctuation, Others :Characters; A, C, S, N, P, O, Counter :INTEGER; Ch :CHAR; BEGIN { Link the file variable to the file name 'C:\\CONFIG.SYS' } ASSIGN(DiskFile, 'C:\\CONFIG.SYS'); { Open the file for input } RESET(DiskFile); { The program logic } Counter := 0; { counter of all characters } A := 0; { counter of alphabetic characters } C := 0; { counter of capital letters } S := 0; { counter of small letters } N := 0; { counter of numeric characters } P := 0; { counter of punctuation characters } O := 0; { counter of other characters } Small := ['a'..'z']; Capital := ['A'..'Z']; Alphabet := Small + Capital; Numerals := ['0'..'9']; Punctuation := [',',';','-','''','.','!','?',')','(','"',':','_']; { Check for the end of the disk file } WHILE NOT EOF(DiskFile) DO BEGIN { Check for the end of line in the disk file } WHILE NOT EOLN(DiskFile) DO BEGIN { Read one character from the disk file } READ(DiskFile,Ch); Counter:= Counter + 1; IF Ch IN Alphabet THEN BEGIN A:= A + 1; IF Ch IN Small THEN S:= S + 1 ELSE IF Ch IN Capital THEN C:= C + 1 END ELSE IF Ch IN Numerals THEN N:= N + 1 ELSE IF Ch IN Punctuation THEN P:= P + 1 ELSE O:= O + 1 END; { Advance the pointer to the next line } READLN(DiskFile) END; { End of the file is reached } { Close the file } CLOSE(DiskFile); { Display the report } WRITELN; WRITELN('Total number of characters = ', Counter); WRITELN('Number of alphabetic characters = ', A); WRITELN('.Number of lowercase letters: ', S); WRITELN('.Number of uppercase letters: ', C); WRITELN('Number of numeric characters = ', N); WRITELN('Number of punctuation characters = ', P); WRITELN('Number of other characters = ', O); WRITELN('Press ENTER to continue..'); READLN END. Listing of the file CONFIG.SYS: 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 The program output is: Total number of characters = 129 Number of alphabetic characters = 96 .Number of lowercase letters: 9 .Number of uppercase letters: 87 Number of numeric characters = 11 Number of punctuation characters = 7 Number of other characters = 15
|
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. |