[comp.lang.pascal] TP error while opening R/O files

ajbanck@cs.ruu.nl (Arent Banck) (05/02/91)

Using Turbo Pascal 6.0, I wanted to open a file witch was 
write protected. I used RESET to open the file, but ended here
with a error saying I couldn't open a file.
Why can't I open the file? And is there a way to open it?
I am using 4dos, and msdos 5.0. and have not tried it with command.com

Arent Banck.
Email : ajbanck@cs.ruu.nl     Voice : 030-512834
					      (Holland)
-- WARNING!!! The above opinions may be HAZARDOUS or FATAL if swallowed!!! --

ts@uwasa.fi (Timo Salmi) (05/03/91)

In article <1991May02.160542.28060@cs.ruu.nl> ajbanck@cs.ruu.nl (Arent Banck) writes:
>Using Turbo Pascal 6.0, I wanted to open a file witch was 
>write protected. I used RESET to open the file, but ended here
>with a error saying I couldn't open a file.
>Why can't I open the file? And is there a way to open it?
:

You first have to set FileMode as 0 from its default value of 2 in
order to be able to read a readonly file.  The information is in the
manual (at least for 5.0), but then it is pretty well hidden. 

...................................................................
Prof. Timo Salmi
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

frimp@mcshh.hanse.de (Frank Heinzius) (05/06/91)

In <1991May02.160542.28060@cs.ruu.nl> ajbanck@cs.ruu.nl (Arent Banck) writes:

]Using Turbo Pascal 6.0, I wanted to open a file witch was 
]write protected. I used RESET to open the file, but ended here
]with a error saying I couldn't open a file.
]Why can't I open the file? And is there a way to open it?
]I am using 4dos, and msdos 5.0. and have not tried it with command.com

You must reassign the "filemode" system variable like this:

temp := filemode;
filemode := 0;
reset(read_only_file);
{other file operations}
{...}
close(read_only_file);
filemode := temp;


-- 
Real : Frank Heinzius             Voice: +49 40 814 416 
Smart: frimp@mcshh.hanse.de       Blah : 2B or not 2B, that's FF

dave@tygra.Michigan.COM (David Conrad) (05/09/91)

In article <9144@mcshh.hanse.de> frimp@mcshh.hanse.de (Frank Heinzius) writes:
>
>[In response to a question about accessing read only files]
>
>You must reassign the "filemode" system variable like this:
>
>temp := filemode;
>filemode := 0;
>reset(read_only_file);
>{other file operations}
>{...}
>close(read_only_file);
>filemode := temp;

Incidentally, filemode need not be changed the whole time the read only file
is opened, only while opening (i.e., resetting,) it.  It is best to always
open files you only intend to read with filemode set to 0, e.g.

  filemode := 0;  (* open input file in read mode *)
  reset (infile);
  filemode := 2;  (* open output file in read/write mode *)
  reset (outfile);
  while not eof(infile) do
    (* do it! *)
  close (infile);
  close (outfile);

If you try to open a file with filemode = 2, even if you reset, not rewrite
it, even if you only read from it, you will get a runtime error (access
denied).  DOS won't allow a read only file to be opened in read/write mode,
and rightly so.  (BTW, 2 is the default for filemode, so if you don't set it
to anything, 2 (read/write) is what you get.)  Even if you expect a file not
to be read only, if you only want to read it, it's best to let the operating
system know that.

Why?  Well, a user may mark a file which she knows is strictly an input file
read only to prevent it getting clobbered.  Do you want your application to
crash then?  Or perhaps your program gets loaded onto a network.  A user
might not have write priviledges in the current directory, which means that
your program will crash even if the specific file isn't marked read only,
because the network won't allow the current user to open *any* of the files
in the current directory for writing.
 
Also, for networks and file sharing, it's important that the network know
just exactly how the file is to be used, but that is, as they say, beyond
the scope of this article.  Also I should mention that this article is
mostly not a response to Frank's article, but just general info.

David R. Conrad     |   "A dream unthreatened by the morning light
dave@michigan.com   |   could blow this soul right through the roof
...!tygra!dave      |   of the night." -- Pink Floyd, "Learning to Fly"
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave@Michigan.COM