The TEXT_IO Package contains constructs that provide ways to write and read information to and from files. There are several procedures and functions available in Text_IO, falling into the following categories:
If you want to do... | Use these available functions... |
---|---|
file operations | The FILE_TYPE record, the FOPEN and IS_OPEN functions, and the FCLOSE procedure enable you to define FILE_TYPE variables, open files, check for open files, and close open files, respectively. |
output (write) operations | The PUT, PUTF, PUT_LINE, and NEW_LINE procedures enable you to write information to an open file or output it to the Interpreter. |
input (read) operations | The GET_LINE procedure enables you to read a line from an open file. |
Below is an example of a procedure that echoes the contents of a file. Notice that the procedure includes several calls to TEXT_IO constructs:
PROCEDURE echo_file_contents IS
in_file Text_IO.File_Type;
linebuf VARCHAR2(1800);
filename VARCHAR2(30);
BEGIN
filename:=GET_FILE_NAME('c:/temp/', File_Filter=>'Text Files (*.txt)|*.txt|');
in_file := Text_IO.Fopen(filename, 'r');
LOOP
Text_IO.Get_Line(in_file, linebuf);
:text_item5:=:text_item5||linebuf||chr(10);
--Text_IO.New_Line;
END LOOP;
EXCEPTION
WHEN no_data_found THEN
Text_IO.Put_Line('Closing the file...');
Text_IO.Fclose(in_file);
END;