modsquad
2010-04-01 21:34:48 UTC
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
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