Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Extract a binary file from LONG field?

Re: Extract a binary file from LONG field?

From: mark tomlinson <marktoml_at_gdi.net>
Date: Tue, 14 Apr 1998 16:39:20 GMT
Message-ID: <353390e7.443591941@newshost.us.oracle.com>


Using V7.3 OCI from 'C'...

/*
 * This sample uses the table FILES which is defined as follows:
 *   create table FILES (name VARCHAR2(20), len NUMBER, data LONG
RAW);
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <ociapr.h>

// Define the OCI datatypes
#define VARCHAR2_TYPE 1
#define NUMBER_TYPE 2
#define INT_TYPE 3
#define FLOAT_TYPE 4
#define STRING_TYPE 5
#define ROWID_TYPE 11
#define DATE_TYPE 12
#define LONG_RAW 24
#define LONG_VARRAW 95

void main()
{

        Cda_Def lda;
        Cda_Def cda;
        char    hda[256];

        FILE*   fhndl;

        // Data variables
        char*   filedat;                // The acutal raw data
        int     fileread;               // The length of the raw data
read in
        int     filelen;                // The length of the file
        char    filenam[20];    // The name of the file
        int     filepos;                // The offset marker in the
LONG RAW column
        int     filechnk;               // The size of the file chunk
fetched
        // Indicator variables
        short   i_filelen = 0;
        short   i_filenam = 0;
        short   i_filedat = 0;

        // Open the input file
        strcpy(filenam,"MYFILE.EXE");
        fhndl = fopen(filenam, "rb");

        // Determine the length of the file
        filelen = filelength(fileno(fhndl));

        // Allocate memory for the file buffer plus 4 bytes for OCI
long raw handling
        filedat = (char*) malloc(filelen+4);
        memset(filedat,0,filelen+4);

        // Set the first four bytes of the buffer to the file length
        (*(long*)filedat) = (long)filelen;

        // Read in the raw data from the file at offset 4
        fileread = fread( filedat+4, 1, filelen, fhndl );
        fclose(fhndl);

        // Check to ensure that the filelenght equals the lenght of
the data read in
        if (fileread != filelen)
        {
                printf("Error reading file ...");
                return;
        }

        // Insert the file using OCI
        memset(hda,0,255);
        if

(olog(&lda,(text*)&hda,(text*)"scott/tiger_at_t:orlnt-5:ORCL",-1,0,-1,0,-1,OCI_LM_DEF))

        printf( "Logon: %i", lda.rc );

        if (oopen(&cda,&lda,0,-1,-1,0,-1)) 
        printf( "Open: %i", cda.rc );

        if (oparse(&cda,
                (text*)"insert into FILES values
(:filenam,:filelen,:filedat)",
                -1,1,2))
        printf( "Parse: %i", cda.rc );

        if

(obndra(&cda,(text*)":filenam",-1,filenam,strlen(filenam)+1,STRING_TYPE,-1,
                &i_filenam,0,0,0,0,0,-1,-1))
        printf( "Bind: %i", cda.rc );

        if

(obndra(&cda,(text*)":filelen",-1,(text*)&filelen,sizeof(int),INT_TYPE,-1,
                &i_filelen,0,0,0,0,0,-1,-1))
        printf( "Bind: %i", cda.rc );

        // We are binding as a LONG VARRAW in order to allow for files
> 64k
        if
(obndra(&cda,(text*)":filedat",-1,(text*)filedat,-1,LONG_VARRAW,-1, 
                &i_filedat,0,0,0,0,0,-1,-1))
        printf( "Bind: %i", cda.rc );

        if (oexec(&cda))
        printf( "Exec: %i", cda.rc );

        if (ocom(&lda))
        printf( "Commit: %i", lda.rc );

        if (ocan(&cda))
        printf( "Cancel: %i", lda.rc );

        if (ologof(&lda)) 
        printf( "Logof: %i", lda.rc );


        // Release the buffer associated with filedat
        free(filedat);

        // Fetch the file using OCI
        memset(hda,0,255);
        if

(olog(&lda,(text*)&hda,(text*)"scott/tiger_at_t:orlnt-5:ORCL",-1,0,-1,0,-1,OCI_LM_DEF))

        printf( "Logon: %i", lda.rc );

        if (oopen(&cda,&lda,0,-1,-1,0,-1)) 
        printf( "Open: %i", cda.rc );

        if (oparse(&cda,
                (text*)"select NAME, LEN from FILES",
                -1,1,2))
        printf( "Parse: %i", cda.rc );

        if
(odefin(&cda,1,(text*)&filenam,20,STRING_TYPE,-1,&i_filenam,0,0,-1,0,0))
        printf( "Define: %i", cda.rc );

        if
(odefin(&cda,2,(text*)&filelen,sizeof(int),INT_TYPE,-1,&i_filelen,0,0,-1,0,0))
        printf( "Define: %i", cda.rc );

        if (oexfet(&cda,1,0,0))
        printf( "Exec Fet: %i", cda.rc );

        // Allocate memory for the file buffer
        filedat = (char*) malloc(65535);        // We will fetch in
64k chunks
        memset(filedat,0,65535);

        if (oparse(&cda,
                (text*)"select DATA from FILES",
                -1,1,2))
        printf( "Parse: %i", cda.rc );

        // We are binding as a LONG RAW since we know the length from
the table
        if
(odefin(&cda,1,filedat,65535,LONG_RAW,-1,&i_filedat,0,0,-1,0,0))
        printf( "Define: %i", cda.rc );

        if (oexfet(&cda,1,0,0))
        printf( "Exec: %i", cda.rc );

        // Open the ouput file
        strcpy(filenam,"OFILE.EXE");    // Change the output file name
for demo
        fhndl = fopen(filenam, "wb");

        // We already have our first chunk so write to to file
        fwrite(filedat,1,65535,fhndl);

        // The cursor is on the current record so fetch the column
        filepos = 65535;
        while (filepos < filelen)
        {
                if
(oflng(&cda,1,filedat,65535,LONG_RAW,&filechnk,filepos))
                printf( "Exec: %i", cda.rc );
                // Write this chunck to file
                fwrite(filedat,1,filechnk,fhndl);
                filepos += filechnk;
        }

        fclose(fhndl);

        if (ocan(&cda))
        printf( "Cancel: %i", lda.rc );

        if (ologof(&lda)) 
        printf( "Logof: %i", lda.rc );

        return;

} Received on Tue Apr 14 1998 - 11:39:20 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US