Jump to content

Category:ALGOL 68-files

From Rosetta Code
Library
This is an example of a library. You may see a list of other libraries used on Rosetta Code at Category:Solutions by Library.

ALGOL 68-files is a set of file related modes and operators for use in ALGOL 68 programs.

To use this in a program, copy the source code from the Talk page to a text file called files.incl.a68, putting it in the same directory as the importing program.
Then add PR read "files.incl.a68" PR to the source of the program.

If you are using ALGOL_68G or another compiler/interpreter that supports the read pragmatic comment, then the program can be run as usual.
For other compilers, the ALGOL 68 pre-processor in Compiler/Simple file inclusion pre processor#ALGOL 68 can be used (see the instructions on that page).


Operators

The following operators are defined in this library:

  • OP (STRING,PROC(STRING)VOID)INT EACHLINE
applies the PROC(STRING)VOID to each line of the file named in the first parameter.
Returns the number of lines in the file or -1 if the file couldn't be opened.
When called, the parameter of the procedure will contain the latest line from the file.
  • OP (STRING,PROC(STRING,INT)BOOL)INT EACHLINE
applies the PROC(STRING,INT)BOOL to each line if the file named in the first parameter.
Returns the count of lines where the procedure returned TRUE or -1 if the file couldn't be opened.
When called, the first parameter of the procedure will contain the line, the second parameter will contain the number of preceding lines where the procedure returned TRUE.
  • OP (STRING)INT PRINTLINES
Prints the contents of the named file to stand out.
Returns the number of lines in the file or -1 if it couldn't be opened.
  • OP (STRING)REF[]STRING READLINES
Returns an array containing the lines in the named file.
The array will be empty if the file could not be opened.
  • OP (STRING,[]STRING)BOOL APPENDLINES
Overwrites the named file, extending it with the lines specified in the array.
The file is created if it does not exist.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.