plsqlutl-fileplsql-package

PL/SQL UTL_FILE: How the loop automatically returns the next line?


This is PL/SQL code I am using for working with UTL_FILE package:

DECLARE
    vfile   utl_file.file_type;
    vfloc   VARCHAR2(100) := 'UTL_TST_DIRC';
    vline   VARCHAR(32676);
BEGIN
    vfile := utl_file.fopen(vfloc, 'test.txt', 'r', 32767);
    LOOP
        utl_file.get_line(vfile, vline, 32767);
        dbms_output.put_line(vline);
        --here i am not doing any incremental operation but i am getting the next line automatically. How?
    END LOOP;

EXCEPTION
    WHEN no_data_found THEN
        utl_file.fclose(vfile);
        NULL;
END;
/

The file I am reading has the below content

This is line 1.
This is line 2.
This is line 3.
This is line 4.

The output I get running the above script is as below which is perfect.

This is line 1.
This is line 2.
This is line 3.
This is line 4.

But what I am not clear with is, How the 'utl_file.get_line()' functions automatically returns the next line in the next iteration of the loop. Thanks in advance.


Solution

  • Every call of utl_file.get_line advances the file pointer by the amount of characters read. If the read line is terminated by a line terminator, the file pointer is advanced over that line terminator, even though the line terminator is not returned by get_line.