Previous Top Next

File Functions

+ General + Files can be referenced as a string containing the name + of the file ie "TEST.R" or the file handle that is + returned from OPEN function. (Normally the second way + if prefered when you want to open 2 or more files with + the same name). + There are always 5 special files: + Handle - filename Description + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 0 <STDIN> Standard input + 1 <STDOUT> Standard output + 2 <STDERR> Standard error + 3 <STDAUX> Standard auxiliary file, ie COM1: + 4 <STDPRN> Standard printer file, ie LPT1: + All open files are closed at the end of the program + from REXX interpreter except in the case of error + CLOSE( file ) + closes an opened file. + file may be string or the handle number call close 'BINGO' /* these two cmds are exactly the same */ call close hnd /* where hnd=open('BINGO','w') */ + EOF( file ) + returns 1 at eof, -1 when file is not opened + and 0 otherwise if eof(hnd) then signal finito + FLUSH( file ) + flush a stream to disk call flush 'BINGO' + OPEN( file, mode ) + opens a file. (mode follows C prototypes: + "r" - for read, "w" - for write, "t" - for text, + "a" - for append, "b" - for binary, "+" - to + open file for read/write ) + and returns the handle number for that file + -1 if file is not found! hnd = open('BINGO','w') if hnd = -1 then do say 'error opening file BINGO.' ... end + READ( (file)(,length) ) + returns reads length bytes from file. + o if length is not specified then return one line from file + o if file is not opened, it will be opened automatically + in "r" mode + o if file is ommited, it is assumed to be <STDIN> kbdin = read() /* reads one line from stdin */ keypressed = read(,1) /* -//- char -//- */ linein = read('BINGO') /* reads one line from file */ linein = read(hnd) /* -//- */ ch = read('data',1) /* if file 'data' is not opened then it will be opened in "r" mode and ch will contain one character.*/ + SEEK( file (,offset (,"TOF","CUR","EOF"))) + move file pointer to offset relative from TOF (default) + and return new file pointer + * this is an easy way to determine the filesize, + by seeking at the end, filesize = seek(file,0,"EOF") call seek 'data',0,"TOF" /* sets the pointer to the start of the file */ filepos = seek('data',-5,"CUR") /* moves pointer 5 bytes backwards, and returns current pos */ + WRITE( (file)(, string(,))) + writes the string to file. + returns the number of bytes written + * if a comma is specified as a third argument then + newline is added at the end of the string + WARNING: comma at the end of a line causes line continuation! This can be avoided either with a semicolon directly after the comma, or with any variable as a third argument that it will be ommited. ie. call write 'data','First line',; /* note the semicolon */ or call write 'data','Second line',nl + * if neither string is specified then results to one + newline in the file + * if file is not opened, it will be opened automatically + in "w" mode + * if file is ommited, it is assumed to be <STDOUT> call write ,'a' /* writes 'a' to stdout */ call write '','one line',nl /* write 'one line' to stdout */ call write 'data','bingo' /* writes 'bingo' to 'data' file*/

Previous Top Next