Home » SQL & PL/SQL » SQL & PL/SQL » Updating a File with UTL_FILE (Oracle 10g2, Solaris)
Updating a File with UTL_FILE [message #453888] Mon, 03 May 2010 00:08 Go to next message
mjm22
Messages: 44
Registered: January 2010
Location: Singapore
Member
Hi,

I have an application that creates files using the utl_file package. It works fine but one of the lines in the file should hold the number of bytes in the file (on a line formatted like 'FileSize: 2104'). Unfortunately this line is not the last line of the file and the lines that follow it are variable in length.

My approach therefore is as follows:
1. Write the 'FileSize: ' line during file creation.
2. Write the remaining lines of data to the file (but don't close it).
3. Use utl_fgetattr to find the file size.
4. Go back and find the 'FileSize' line I need to update, using get_line to read it into the buffer.
5. Append the filesize (plus the number of characters that the variable I use to store the filesize value) to the buffer string.
6. Write the line back to the file using put_line.

However I keep getting invalid file operation errors however I try to re-access the file...

PROCEDURE setUtlFileSize (pFileName   IN VARCHAR2,
                          pFileHandle IN utl_file.file_type) IS

    vbFileExists    BOOLEAN;
    viFileLen       NUMBER;
    viFileBlockSize NUMBER;
    vsFileRecord    VARCHAR2(2000);

BEGIN

    utl_file.fgetattr('MJMEXPDIR',pFileName,
                        vbFileExists,viFileLen,viFileBlockSize);

    dbms_output.put_line('File Length: ' || TO_CHAR(viFileLen));

    IF utl_file.is_open(pFileHandle )
    THEN

        BEGIN
            viCount := utl_file.fgetpos(pFileHandle );
            dbms_output.put_line('File Position: ' || TO_CHAR(viCount));

            utl_file.get_line(pFileHandle, vsFileRecord);
            dbms_output.put_line('Rec Data: ' || vsFileRecord);


        EXCEPTION
            WHEN OTHERS THEN
                dbms_output.put_line(SQLCODE || ',' || SQLERRM);

        END;

    END IF;
END;


This gives me the following output.... (for two files)... currently I am using some generic exception handling just to show me the error.
File Length: 2106
File Position: 0
-29283,ORA-29283: invalid file operation
File Length: 497
File Position: 0
-29283,ORA-29283: invalid file operation
Process exited.


Two things here concern me: The File Position suggests that the current offset position is set to the start of the file... but since I had not closed or performed any other operation since the last put_line and fflush (which are used to add lines to the file) I had expected that the offset position would be the same as the file length??
Secondly: Even if the position had been reset to the start of the file I don't understand why the get_line gives me the oracle error.
I appreciate any ideas here. Thanks in advance.
Re: Updating a File with UTL_FILE [message #453923 is a reply to message #453888] Mon, 03 May 2010 01:54 Go to previous message
flyboy
Messages: 1670
Registered: November 2006
Senior Member
Hi,

I am afraid you will have to change your tool/approach.

You might be interested in reading this thread: http://www.orafaq.com/forum/mv/msg/151759/428942/0/#msg_428942.
The error is quite confusing, anyway (as I understand it), you are not allowed to read data when the file is open in write/append mode. It shall be open in read mode, however, in that case, you are not able to write to the file. If opening a file in both modes (read+write) would be possible, then... but I am afraid it is not.
Previous Topic: Left outer join Vs. Right outer join (merged)
Next Topic: multiple join to the same table
Goto Forum:
  


Current Time: Tue May 21 17:53:13 CDT 2013

Total time taken to generate the page: 0.99460 seconds