ibm-information-center/dist/eclipse/plugins/i5OS.ic.apiref_5.4.0.1/apiexwofil.htm

231 lines
9.5 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Working with stream files" />
<meta name="abstract" content="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." />
<meta name="description" content="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." />
<meta name="DC.Relation" scheme="URI" content="apiexmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="apiexwofil" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Working with stream files</title>
</head>
<body id="apiexwofil"><a name="apiexwofil"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Working with stream files</h1>
<div><p>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.</p>
<div class="section"><p>The program uses the following hierarchical file system (HFS)
APIs:</p>
<ul><li>Create Directory (QHFCRTDR) </li>
<li>Open Stream File (QHFOPNSF) </li>
<li>Read from Stream File (QHFRDSF) </li>
<li>Close Stream File (QHFCLOSF)</li>
</ul>
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code license and disclaimer information</a> for important
legal information.</div>
<pre>/********************************************************************/
/* 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;qhfopnsf.h&gt;
#include &lt;qhfrdsf.h&gt;
#include &lt;qhfclosf.h&gt;
#include &lt;qhfcrtdr.h&gt;
#include &lt;qusec.h&gt;
/********************************************************************/
/* 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-&gt;EC.Exception_Id;
printf("Bytes Provided -&gt; %d\n",theErrCode-&gt;EC.Bytes_Provided);
printf("Bytes Available -&gt; %d\n",theErrCode-&gt;EC.Bytes_Available);
printf("Exception ID -&gt; ");
for (i=0;i&lt;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,&amp;attrib_info,attrib_length,&amp;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(&amp;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(&amp;file_handle,
path_name,
path_length,
open_info,
&amp;attrib_info,
attrib_length,
&amp;action,
&amp;error_code);
if (error_code.EC.Bytes_Available != 0)
{
printErrCode(&amp;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(&amp;file_handle,
read_buffer,
bytes_to_read,
&amp;bytes_read,
&amp;error_code);
if (error_code.EC.Bytes_Available != 0)
{
cmpgood = strncmp("CPF1F33",error_code.EC.Exception_Id,7);
if (cmpgood != 0)
printErrCode(&amp;error_code);
end_file = ON;
}
else
{
printf("BYTES READ: %d\n ",bytes_read);
printf("READ BUFFER: %s\n",read_buffer);
if (bytes_read &lt; bytes_to_read)
{
end_file = ON;
/*******************************************************/
/* Write remaining bytes to the database file */
/*******************************************************/
if (bytes_read &gt; 0)
fwrite(read_buffer,1,bytes_read,FP);
}
}
}
/********************************************************************/
/* Close the stream file */
/********************************************************************/
printf("CLOSE STREAM FILE:\n ");
QHFCLOSF(&amp;file_handle,
&amp;error_code);
if (error_code.EC.Bytes_Available != 0)
printErrCode(&amp;error_code);
/********************************************************************/
/* Close the database file */
/********************************************************************/
fclose(FP);
}</pre>
<p>To create the program using ILE C, specify the following:</p>
<pre>CRTBNDC PGM(QGPL/HFSCOPY) SRCFILE(QGPL/QCSRC)</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="apiexmp.htm" title="Contains example programs that use APIs and exit programs.">Examples: APIs</a></div>
</div>
</div>
</body>
</html>