menu prev next

End of File and End of Line

EOF
Accepts the name of the input file, and returns true if there is no more data to be read.

EOLN
Accepts the name of the input file, and is true if there are no more characters on the current line.

When reading information from a text file, the character which is read can be compared against EOLN or EOF. Consider the following program which displays the contents of a text file on the console screen.


	program  SHOWTEXT ( infile, input, output );
	var   ch : char;
	    fname : packed array [1..15] of char;
	    infile: TEXT;
	begin
	    writeln('Please enter name of text file to display.');
	    readln( fname );

	    reset( infile, fname );     {open a file using filename stored in}
	                                {array fname                         }
	    while not eof( infile ) do
	    begin
	       while not eoln( infile ) do
	       begin
	           read( infile, ch );
	           write( ch )
	       end;
	       readln( infile );       {read  eoln character}
	       writeln                 {write eoln character}
	    end;
	    close( infile )             {close filename specified by fname}
	end.


PROGRAM TWENTY-ONE
Write a program to count the number of characters in a text file. The valid characters are 'A' to 'Z', and 'a' to 'z'.

Click here for answer

PROGRAM TWENTY-TWO
Write a program to count the number of words in a text file.

Click here for answer


Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.
menu prev next

1