File IO

After migrating one of our shares, we’ve gone through a bumpy transition period for Oracle File I/O. After reviewing some of the code I thought it noteworthy enough to post.

I particularyly dislike having to create a new ‘block’ denoted by the begin statement. You loop through the file and read the next line. Basically you do this ‘forever’ until the program throws a no_data_found exception. You then exit the block and move on.

Weird. Haven’t we heard of EOF()? Seriously. Why can’t we just loop until we’ve reached the end of the file. That’s just plain weird. No sir I don’t like it. But there it is in it’s … uh… glory.


— Open the file
vfile := sys.utl_file.fopen( cpath||’directory’, ‘file.txt’, ‘r’);

loop

— Believe it or not, this is proper file looping structure.
— Step 1: Read a line
— Step 2: If there is no more data, just exit the loop
— Sounds quirky and really piece-meal, but it works…

begin
sys.utl_file.get_line(vfile, vline);
exception
when no_data_found then
exit;
end;

end loop

Leave a Reply