Discussion:
Modula-2 Handbook p.28 example 6.1.3.4 "Writing a Text File" will overwrite existing file
(too old to reply)
modsquad
2010-04-01 21:34:48 UTC
Permalink
RE: "Modula-2 Handbook" by Modula-2 Reearch Institute, as
redistributed by CFB Software
physical page 28, example 6.1.3.4 "Writing a Text File" (using
FileSystem)

The text is obviously in error because the inner IF conditional to
test for f.res = done is embedded in the outer IF test for f.res <>
done, which obviously means the inner IF is never reached.

I recall once seeing a printed version of the Modula-2 Handbook,
(which unfortunately I cannot now locate),
and from memory I believe that it read (* minus "my comment"s added
below *):
...
Lookup(f, "newfile", TRUE);
IF (f.res <> done) OR NOT(f.new) THEN
(* f was not created by this call to "Lookup" *)
IF f.res = done THEN
(* my comment: f already exists *)
Close(f)
END (* if *)
ELSE
(* my comment: f newly created *)
LOOP
...
END (* loop *);
...
END (* if *)
...
I believe this example was intended to prevent overwriting a pre-
existing output file, since I believe Lookup with the NEW parameter
will open an existing file without truncation and set the write
position to 0. It only creates a new file if it does not already
exist.

In fact, N. Wirth, in PIM2, 3rd Edition, page. 113, recommends
checking the "f.new" field to prevent overwriting an existing file,
although he did not give a full example of how to do the check, as I
believe the "Modula-2 Handbook" did at one time in at least one
version.

I have checked the corrected version using Logitech 3.4 Modula-2
FileSystem library module. But I have not checked this on either M2M-
PC 1.35 or the Emulith system.or anything else.

Of course, overwriting an existing file may be desired in some cases,
but I believe that only InOut.OpenOutput warns you and asks for
confirmation before overwriting (at least in the Logitech version).

Since FileSystem.Lookup does not offer the same protection (unless you
program it yourself), I thought I should warn other Modulans to be
careful to check the output file variables "new" field.

Sincerely
Carl
Pascal J. Bourguignon
2010-04-02 08:57:19 UTC
Permalink
Post by modsquad
RE: "Modula-2 Handbook" by Modula-2 Reearch Institute, as
redistributed by CFB Software
physical page 28, example 6.1.3.4 "Writing a Text File" (using
FileSystem)
The text is obviously in error because the inner IF conditional to
test for f.res = done is embedded in the outer IF test for f.res <>
done, which obviously means the inner IF is never reached.
I recall once seeing a printed version of the Modula-2 Handbook,
(which unfortunately I cannot now locate),
and from memory I believe that it read (* minus "my comment"s added
...
Lookup(f, "newfile", TRUE);
IF (f.res <> done) OR NOT(f.new) THEN
(* f was not created by this call to "Lookup" *)
This assertion is wrong. It should be:
(* f was not created by this call to "Lookup"
OR f.res<>done *)
Post by modsquad
IF f.res = done THEN
(* my comment: f already exists *)
Indeed. Since:

( f was not created by this call to "Lookup"
OR f.res<>done ) AND f.res = done

<=> ( f was not created by this call to "Lookup" AND f.res = done )
OR ( f.res<>done AND f.res = done)

<=> ( f was not created by this call to "Lookup" AND f.res = done )
OR FALSE

<=> ( f was not created by this call to "Lookup" AND f.res = done )

==> ( f was not created by this call to "Lookup" )
Post by modsquad
Close(f)
END (* if *)
ELSE
(* my comment: f newly created *)
LOOP
...
END (* loop *);
...
END (* if *)
Since I don't have the text refered at hand, I cannot further comment,
but if it has, as in your "from memory" code, a disjonction in the outer
IF, then the inner IF may not be useless.
--
__Pascal Bourguignon__
Chris Burrows
2010-04-02 12:13:25 UTC
Permalink
Post by modsquad
I recall once seeing a printed version of the Modula-2 Handbook,
(which unfortunately I cannot now locate),
and from memory I believe that it read (* minus "my comment"s added
...
Lookup(f, "newfile", TRUE);
IF (f.res <> done) OR NOT(f.new) THEN
I think you are right. That is how it is written in Section 9.1.3.4 of the
'Lilith Handbook' that the Modula-2 Handbook was derived from.

--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2
modsquad
2010-04-02 20:33:22 UTC
Permalink
Thanks for jogging my memory.

I saw the correct version in the printed Lilith Handbook from Modula
Research Institute that they used to sell for 25USD in a three ring
binder.

Carl
----
Post by Chris Burrows
Post by modsquad
I recall once seeing a printed version of the Modula-2 Handbook,
(which unfortunately I cannot now locate),
and from memory I believe that it read (* minus "my comment"s added
...
Lookup(f, "newfile", TRUE);
IF (f.res <> done) OR NOT(f.new) THEN
I think you are right. That is how it is written in Section 9.1.3.4 of the
'Lilith Handbook' that the Modula-2 Handbook was derived from.
--
Chris Burrows
CFB Softwarehttp://www.cfbsoftware.com/modula2
Loading...