Example of file control

This sample program demonstrates how to use the IccFile and IccFileIterator classes.

The source for this sample can be found in C++ sample programs , in file ICC$FIL. Here the code is presented without any of the terminal input and output that can be found in the source file.
#include "icceh.hpp"
#include "iccmain.hpp"
The first two lines include the header files for the Foundation Classes and the standard main function which sets up the operating environment for the application program.
const char* fileRecords[] =
{
//NAME KEY PHONE USERID
"BACH, J S 003 00-1234 BACH ",
"BEETHOVEN, L 007 00-2244 BEET ",
"CHOPIN, F 004 00-3355 CHOPIN ",
"HANDEL, G F 005 00-4466 HANDEL ",
"MOZART, W A 008 00-5577 WOLFGANG "
};
This defines several lines of data that are used by the sample program.
void IccUserControl::run()
{
The run method of IccUserControl class contains the user code for this example. As a terminal is to be used, the example starts by creating a terminal object and clearing the associated screen.
 short recordsDeleted = 0;
IccFileId id("ICCKFILE");
IccKey key(3,IccKey::generic);
IccFile file( id );
file.registerRecordIndex( &key );
key = "00";
recordsDeleted = file.deleteRecord();
The key and file objects are first created and then used to delete all the records whose key starts with "00" in the KSDS file "ICCKFILE". key is defined as a generic key having 3 bytes, only the first two of which are used in this instance.
 IccBuf buffer(40);
						key.setKind( IccKey::complete );
						for (short j = 0; j < 5; j++)
						{
						buffer = fileRecords[j];
						key.assign(3, fileRecords[j]+15);
						file.writeRecord( buffer );
						}

This next fragment writes all the data provided into records in the file. The data is passed by means of an IccBuf object that is created for this purpose. setKind method is used to change key from 'generic' to 'complete'.

The for loop between these calls loops round all the data, passing the data into the buffer, using the operator= method of IccBuf , and thence into a record in the file, by means of writeRecord. On the way the key for each record is set, using assign , to be a character string that occurs in the data (3 characters, starting 15 characters in).
 IccFileIterator fIterator( &file,
						&key );
						key = "000";
						buffer = fIterator.readNextRecord();
						while (fIterator.condition() == IccCondition::NORMAL)
						{
						term->sendLine("- record read: [%s]",(const char*) buffer);
						buffer = fIterator.readNextRecord();
						}

The loop shown here lists to the terminal, using sendLine , all the records in ascending order of key. It uses an IccFileIterator object to browse the records. It starts by setting the minimum value for the key which, as it happens, does not exist in this example, and relying on CICS® to find the first record in key sequence.

The loop continues until any condition other than NORMAL is returned.
 key = "\xFF\xFF\xFF";
fIterator.reset( &key );
buffer = fIterator.readPreviousRecord();
while (fIterator.condition() == IccCondition::NORMAL)
{
buffer = fIterator.readPreviousRecord();
}
The next loop is nearly identical to the last, but lists the records in reverse order of key.
 key = "008";
						buffer = file.readRecord( IccFile::update );
						buffer.replace( 4, "5678", 23);
						file.rewriteRecord( buffer );
This fragment reads a record for update, locking it so that others cannot change it. It then modifies the record in the buffer and writes the updated record back to the file.
 buffer = file.readRecord();
The same record is read again and sent to the terminal, to show that it has indeed been updated.
 return;
}

The end of run , which returns control to CICS.

See C++ sample programs for the expected output from this sample.



hxfi013.html | Timestamp icon Last updated: Monday, 5 June 2017