Bookstore

Recent

Author Topic: read() and write() question  (Read 737 times)

440bx

  • Hero Member
  • *****
  • Posts: 1553
read() and write() question
« on: February 09, 2020, 11:38:02 am »
Hello,

Do the standard procedures "read" and "write" do anything if they are called without parameters ? i.e, "read();" and "write();"

Some code is generated but it doesn't seem to have any effect.  The reason I'm asking the question is because I want to make sure I didn't miss some "hidden" effect they may have when called without parameters.

Thank you for your help.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 1487
Re: read() and write() question
« Reply #1 on: February 09, 2020, 02:26:56 pm »
Do the standard procedures "read" and "write" do anything if they are called without parameters ? i.e, "read();" and "write();"
Logically, they should not do anything - there are no arguments, nothing to read/write. Actual implementation: they call the Flush function. For Input, it is not present, but for Output, it is called at the end of any Write operation. So the only possible hidden effect is that if the previous call to Write(ln) failed, Write will flush the buffer.
By the way, in Delphi, for {$IOCHECKS OFF} the code is not generated at all for Read and Write.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8035
Re: read() and write() question
« Reply #2 on: February 09, 2020, 03:12:51 pm »
Afaik write_end is only for textfile, not for typed file . OTOH typed file is buffered less, so that might not matter much .

440bx

  • Hero Member
  • *****
  • Posts: 1553
Re: read() and write() question
« Reply #3 on: February 09, 2020, 06:27:21 pm »
Logically, they should not do anything - there are no arguments, nothing to read/write. Actual implementation: they call the Flush function. For Input, it is not present, but for Output, it is called at the end of any Write operation. So the only possible hidden effect is that if the previous call to Write(ln) failed, Write will flush the buffer.
By the way, in Delphi, for {$IOCHECKS OFF} the code is not generated at all for Read and Write.
That makes sense.  Thank you Serge.

Afaik write_end is only for textfile, not for typed file . OTOH typed file is buffered less, so that might not matter much .
Thank you Marco.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

EganSolo

  • Full Member
  • ***
  • Posts: 152
Re: read() and write() question
« Reply #4 on: February 21, 2020, 05:24:25 am »
That's interesting. I guess I never thought about the difference between read() and readln().
Consider the silly little program:
Code: Pascal  [Select]
  1. Program Project1;
  2. Begin
  3.   Write('Hello ... when ready hit a key on the keyboard for the rest...');
  4.   readln();
  5.   Write(' World!');
  6.   readln();
  7. End.
  8.  

Readln requires no param to work but if you replace the readln() with read(), the code will pass over it as if it wasn't there.

lucamar

  • Hero Member
  • *****
  • Posts: 2561
Re: read() and write() question
« Reply #5 on: February 21, 2020, 12:52:07 pm »
Readln requires no param to work but if you replace the readln() with read(), the code will pass over it as if it wasn't there.

That's quite logical: after all, ReadLn has to wait for an end-of-line before returning even if it's returning an empty line and putting it nowhere. And it was the only way to implement a "Press <Return> to continue" in the good old days. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.