This ILE C program opens an existing stream file, creates or replaces a database file, reads from the stream file and writes to the database file until end-of-file, and closes both files.
The program uses the following hierarchical file system (HFS) APIs:
/********************************************************************/ /* Program Name: HFSCOPY */ /* Language : ILE C */ /* Description : This program will do the following: */ /* -- Create or replace a stream file */ /* -- Create or replace a database file */ /* -- Read from the stream file and write to the */ /* database file until EOF */ /* -- Close both files when done */ /* APIs Used : QHFCRTDR, QHFOPNSF, QHFRDSF, QHFCLOSF */ /********************************************************************/ /********************************************************************/ /* Include files */ /********************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <qhfopnsf.h> #include <qhfrdsf.h> #include <qhfclosf.h> #include <qhfcrtdr.h> #include <qusec.h> /********************************************************************/ /* Structure and variable definitions */ /********************************************************************/ #define ON 1 #define OFF 0 typedef struct error_code_struct { Qus_EC_t EC; char exception_data[256]; }error_code_struct; error_code_struct error_code; char file_handle[16]; char path_name[30]; char open_info[10]; char attrib_info; char action; char read_buffer[80]; int path_length; int attrib_length = 0; int bytes_to_read; int bytes_read = 0; int end_file; int cmpgood; FILE *FP; /*********************************************************************/ /*printErrCode: Routine to print the error code structure */ /*********************************************************************/ void printErrCode(error_code_struct *theErrCode) { int i; char *tempptr = theErrCode->EC.Exception_Id; printf("Bytes Provided -> %d\n",theErrCode->EC.Bytes_Provided); printf("Bytes Available -> %d\n",theErrCode->EC.Bytes_Available); printf("Exception ID -> "); for (i=0;i<7 ;i++,tempptr++ ) { putchar(*tempptr); } putchar('\n'); } /********************************************************************/ /* Start of code */ /********************************************************************/ main() { error_code.EC.Bytes_Provided = 116; /********************************************************************/ /* Create the directory */ /********************************************************************/ strcpy(path_name,"/QDLS/HFSFLR"); path_length = strlen(path_name); QHFCRTDR(path_name,path_length,&attrib_info,attrib_length,&error_code); if ( error_code.EC.Bytes_Available != 0 ) { if (!memcmp(error_code.EC.Exception_Id,"CPF1F04",7)) printf("Directory HFSFLR already created.\n"); else { printErrCode(&error_code); exit(1); } } /********************************************************************/ /* Open the stream file */ /********************************************************************/ strcpy(open_info,"210 120 "); /* Create or replace the file */ strcpy(path_name,"/QDLS/HFSFLR/SAMPLE.HFS"); path_length = strlen(path_name); printf("OPEN STREAM FILE:\n "); QHFOPNSF(&file_handle, path_name, path_length, open_info, &attrib_info, attrib_length, &action, &error_code); if (error_code.EC.Bytes_Available != 0) { printErrCode(&error_code); exit(1); } /********************************************************************/ /* Open a database file */ /********************************************************************/ system("CRTLIB LIB(HFSLIB)"); if (( FP = fopen("HFSLIB/HFSFILE(SAMPLE)","wb")) == NULL) { printf("Cannot open HFSLIB/HFSFILE(SAMPLE)\n"); exit(1); } /********************************************************************/ /* Loop through reading from the stream file and writing to the */ /* database file. */ /********************************************************************/ end_file = OFF; while (end_file == OFF) { /*************************************************************/ /* Read 80 bytes from the stream file */ /*************************************************************/ bytes_to_read = 80; printf("READ STREAM FILE:\n "); QHFRDSF(&file_handle, read_buffer, bytes_to_read, &bytes_read, &error_code); if (error_code.EC.Bytes_Available != 0) { cmpgood = strncmp("CPF1F33",error_code.EC.Exception_Id,7); if (cmpgood != 0) printErrCode(&error_code); end_file = ON; } else { printf("BYTES READ: %d\n ",bytes_read); printf("READ BUFFER: %s\n",read_buffer); if (bytes_read < bytes_to_read) { end_file = ON; /*******************************************************/ /* Write remaining bytes to the database file */ /*******************************************************/ if (bytes_read > 0) fwrite(read_buffer,1,bytes_read,FP); } } } /********************************************************************/ /* Close the stream file */ /********************************************************************/ printf("CLOSE STREAM FILE:\n "); QHFCLOSF(&file_handle, &error_code); if (error_code.EC.Bytes_Available != 0) printErrCode(&error_code); /********************************************************************/ /* Close the database file */ /********************************************************************/ fclose(FP); }
To create the program using ILE C, specify the following:
CRTBNDC PGM(QGPL/HFSCOPY) SRCFILE(QGPL/QCSRC)