home *** CD-ROM | disk | FTP | other *** search
- (***************************************************
- | Source: openfile
- | Purpose: open a text file for reading or writing
- | Date: 7/14/84
- | Author: Jim Ulrick / Berkely, CA / (415)526-0474
- | Source: FOGHORN November 1984, page 49.
- ***************************************************
- | Call this procedure using the following command line:
- | openfile('R',filename,filvar,fileopen) to read file
- | openfile('W',filename,filvar,fileopen) to write file
- | Early in the program, place an include directive for
- | this procedure,($I openfile).
- |
- | Procedure asks for the name of a disk file to open for reading
- | or writing and checks to see if the filename entered
- | already exists. If a file for reading exists it opens it, otherwise
- | it gives an error message and prompts for another name. If
- | a file for writing exists, it asks whether to overwrite the existing file.
- | The user can either overwrite the file, try another filename,
- | or quit. When a file for writing is opened, it is rewritten. The
- | boolean variable 'fileopen' is passed back to the calling
- | procedure and indicates whether a file was successfully opened.
- *****************************************************)
-
- PROCEDURE openfile(filetype: char; var filename: str14;
- var filvar: text; var fileopen: boolean);
- {reading(filetype='R') - writing(filetype='W'}
-
- var
- choice: char;
- exists: boolean;
-
- BEGIN
- fileopen:=false;
- repeat
- gotoxy(6,22); clreol;
- if filetype='R' then write ('Enter name of disk file to read: ')
- else write ('Enter name of disk file to write: ');
- readln(FileName);
- if filename ='' then choice:='Q' {terminate without opening a file}
- else begin
- choice := ' ';
- assign(filvar,FileName);
- {toggle compiler IO error handling and attempt to reset
- file, if the file can be reset then it must exist}
- {$I-} reset(filvar) {$I+};
- exists:=(IOResult = 0); {IOResult is a standard function}
- case filetype of
- 'R': if not exists then begin
- gotoxy(6,23);clreol;
- writeln('Cannot open input file ',filename);
- end
- else
- fileopen:=true;
- 'W': if exists then begin
- gotoxy(6,22);clreol;
- write('File exists, do you want to replace? (Y/N/Quit):');
- repeat
- read(kbd,choice);
- choice:=upcase(choice);
- until choice in ['Y','N','Q',^M];
- {^M is carriage return and is used as a default for Quit}
- if choice='Y' then begin
- fileopen:=true;
- rewrite(filvar);
- end
- end
- else begin
- fileopen:=true;
- rewrite(filvar);
- end;
- end; {case}
- end; {if}
- until fileopen or (choice in ['Q',^M]);
- gotoxy(6,22);clreol;
- gotoxy(6,23);clreol;
- END; {openfile}