I am facing the following strange behaviour of the pascal 'READLN()' and
'READ()' functions on the MPE.
Problem description:
--------------------
There is a file whose record size is greater than 255 bytes. This file can
be of either fixed length or variable length record type. Now a few lines
of text is added to this file. Some of these lines have more than 254
characters in them.
Also there is a Pascal program which I have written, that is supposed to
open the above mentioned file for input and display the contents of that
file line by line (ie. record by record) and also the total number of
characters in each line.
Now I am observing some strange or unexpected behaviour of this small pascal
program under the following instances of input file:
1. If a line in the input file has less than 254 characters, then the
behavior is correct and as expected.
2. If a line in the input file has more than 254 characters, then the
call to the pascal readln() function senses an end of line character
after 254th character and is ignoring the rest of the characters
from 255 till the end of that record. The next call to readln()
reads (at the most 254 chars) FROM THE BEGINNING OF THE NEXT RECORD.
Even the behavior of the pascal function read() is the same as above.
If there are more than 254 characters in a line of record, then
after the 254th call to the read() function the eoln() is becoming
true.
Any explaination for this strange behavior?
Following is the pascal program that I have written, to be executed on MPE:
--------------------------------------------------------------------------
program readGivenFile(input,output);
procedure test;
label
9999;
var
nextChar, ic : char;
tcc,lc : integer; {* tcc => total chars count, lc => line count *}
inFile : text;
inFileName : string[80];
begin
tcc := 0;
lc := 1;
writeln('Enter the name of FILE to read: ');
write(' ');
readln(inFileName);
writeln;
reset(inFile, inFileName); {* open the file for reading *}
{* WHILE not(eof(inFile)) DO BEGIN *}
while(true) do begin
if eof(inFile) then
goto 9999;
if eoln(inFile) then begin
readln(inFile);
writeln;
writeln('Number of chars in line ', lc, ' = ', tcc);
writeln;
lc := lc+1;
tcc := 0;
end {IF}
Else begin
read(inFile, nextChar);
write(nextChar);
tcc := tcc+1;
end; {Else}
end; {WHILE}
9999:
close(inFile);
end;
BEGIN
test;
END.
Also I have coded the above pascal program into an equivalent 'C' program
and ran it on MPE. This equivalent C program is working perfectly fine with
the results as expected!!
Following is the equivalent 'C' code of the above program.
-----------------------------------------------------------
This 'C' program was also executed on the MPE. This program behaves
correctly and gives the expected results.
#include <stdio.h>
main(argc, argv)
int argc;
char* argv[];
{
int tcc,lc,nextChar,ic;
FILE *fp;
if(argc != 2)
{
printf("Usage: %s <fileName>\n", argv[0]);
exit(1);
}
if ( (fp = fopen(argv[1],"r")) == NULL )
{
perror("fopen");
exit(1);
}
tcc=0;
lc=1;
while(1)
{
if( (nextChar = getc(fp)) == EOF)
break;
else
fprintf(stdout,"%c",nextChar);
if(nextChar == '\n')
{
fprintf(stdout,"Number of chars in line %d = %d\n\n", lc,tcc);
lc++;
tcc=0;
}
else
tcc++;
}
fclose(fp);
}
Can anyone explain for this behavior or suggest some solution or workaround
for this Pascal problem on MPE ??
---
Shivanand C. Hiremath
HP-ISO, Bangalore, India.
Email: <[log in to unmask]>,
|