Somebody asked to collect difficult items for Turbo Pascal. Well I have
one : the difference between READ and READLN. I never found out what is
the real difference between them and so I usually try both to see what
gives the best result (mostly READLN does).
Could somebody explain this to me ??
Many thanks,
--Johan
University of Antwerp, Belgium
>Hi,
>Somebody asked to collect difficult items for Turbo Pascal. Well I have
>one : the difference between READ and READLN. I never found out what is
>the real difference between them and so I usually try both to see what
>gives the best result (mostly READLN does).
>Could somebody explain this to me ??
Probeer eens het volgende op een invoerfiletje van twee integers achter
elkaar, bijv. 123 456.
program test(invoer);
var invoer:text;
i,j : integer;
begin
assign(invoer,'<filename>');
reset(invoer);
read(invoer,i);
read(invoer,j);
\* Dit werkt *\
reset(invoer);
read(invoer,i,j);
\* Dit werkt ook *\
reset(invoer);
readln(invoer,i,j);
\* Ook geen probleem *\
reset(invoer);
readln(invoer,i);
readln(invoer,j); \* Hier loopt het mis, de vorige readln had alles *\
\* van de regel gelezen, en alleen de eerste integer *\
\* aan i toegekend, daarna is de pointer in de file *\
\* naar de volgende regel gezet. Hierop staat geen *\
\* integer meer, en zal het programma crashen *\
end.
Dit illustreerd het verschil tussen read en readline wel aardig.
Om het nu precieser te stellen:
read(var1, var2, ..., varn) leest de benodigde waarden voor var1 t/m varn
van een regel (vooropgesteld dat de invoer correct is), en laat de pointer
in de file dan naar de plaats direct achter de waarde wijzen die in varn
is ingelezen wijzen. Staan er nog meer waardes achter, dan kunnen deze
gewoon gelezen worden.
readln(...) doet hetzelfde, echter de rest van de regel wordt overgeslagen
en als er weer gelezen wordt, dan zullen de waardes van de volgende regel
komen.
Succes ermee, Evert.
A
A
A
A
The standard quip applies. I'm having problems with my rot13,
again.
Yes, I know it is Dutch (I even understand random bits of it).
Should I once again seriously consider starting to write my stuff in
Finnish in comp.lang.pascal, since as we've discussed before English
is not compulsory. (Have some sense, please!)
...................................................................
Prof. Timo Salmi (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: t...@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun
The basic difference between the two becomes apparent when you look at
the state of the input buffer. Read advances the input buffer pointer
past the item just read, while ReadLn does the same, but it also
causes the input buffer pointer to point the character following the
next end-of-line.
To help illustrate what happens, suppose we've got the following
program:
program test;
var
a,b,c: Integer;
begin
Write('Enter A :');
Read(A);
Write('Enter B :');
Read(B);
Write('Enter C :');
Read(C);
WriteLn('A=',A:1,' B=',B:1,' C=',C:1);
end.
Here's a sample session (<CR> means you pressed the Enter or Return
key):
C:\TP> TPC TEST
C:\TP> TEST
Enter A: 1<CR>
Enter B: 2<CR>
Enter C: 3<CR>
A=1 B=2 C=3
C:\TP> _
Hmm.. seems just as we expected, right? But let's suppose we try
something else, as follows:
C:\TP> TEST
Enter A: 1 3 5<CR>
Enter B:
Enter C:
A=1 B=3 C=5
C:\TP> _
Now if you may have noticed, the program didn't even stop to let you
enter 'B' nor 'C'. Here's a series of pictures of what the input buffer
looks like when running the program ( *NL* specifies an end of line
marker):
+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*|
+----+----+----+----+----+----+
^
`-- Input buffer Pointer before reading 'A'
+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*|
+----+----+----+----+----+----+
^
`-- Input buffer Pointer after reading 'A'
and before reading 'B'
+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*|
+----+----+----+----+----+----+
^
`-- Input buffer Pointer after
reading 'B'
Suppose we go and change the original program (change all occurrances
of Read to ReadLn):
program test;
var
a,b,c: Integer;
begin
Write('Enter A :');
ReadLn(A);
Write('Enter B :');
ReadLn(B);
Write('Enter C :');
ReadLn(C);
WriteLn('A=',A:1,' B=',B:1,' C=',C:1);
end.
And let's try the same input that the original program gave some
unexpected results.
C:\TP> TEST
Enter A: 1 3 5<CR>
Enter B: 2<CR>
Enter C: 3<CR>
A=1 B=2 C=3
C:\TP> _
Now if you may have noticed, the program now stops to let you enter 'B'
and 'C'. Here's a series of pictures of what the input buffer looks
like when running the program:
+----+----+----+----+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*| 2 |*NL*| 3 |*NL*|
+----+----+----+----+----+----+----+----+----+----+
^
`-- Input buffer Pointer before reading 'A'
+----+----+----+----+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*| 2 |*NL*| 3 |*NL*|
+----+----+----+----+----+----+----+----+----+----+
^
Input buffer Pointer after ----'
reading 'A', before reading 'B'
+----+----+----+----+----+----+----+----+----+----+
Input Buffer | 1 | | 3 | | 5 |*NL*| 2 |*NL*| 3 |*NL*|
+----+----+----+----+----+----+----+----+----+----+
^
Input buffer Pointer after ----'
reading 'B', but before reading 'C'
In reading numeric input, you might not have encountered the difference
between the behaviors of Read and ReadLn, since most of the time you
probably don't type extra data on the input line. If however you were
reading character input, you may have noticed difficulties immediately,
because the newline gets either translated as <CR><LF> (with Turbo
Pascal) or turns into a space (as in standard pascal).
//-n-\\ Naoto Kimura
_____---=======---_____ (abcs...@csuna.csun.edu)
====____\ /.. ..\ /____====
// ---\__O__/--- \\ Enterprise... Surrender or we'll
\_\ /_/ send back your *&^$% tribbles !!