If you want to deal with structured files this may clear up some misconceptions and start you on the right path:
Alternatively, if you want to deal with unstructured streams then you can scrap the SELECT, FD, OPEN, WRITE and CLOSE statements and use subroutines instead. Have a look at page 10-109 in here for details