KCMS CMM Reference Manual

Examples

The following examples show you how to use the KcsFile class.

Reading a File From a Specified Offset

This example shows you how to read a file from a specified offset with absRead(). See the kcsio.h header file for a description of absRead().


Example 2-3 Reading a File From a Specified Offset

KcsStatus readIt()
 {
 	KcsStatus sStatus;
 	KcsFileId sFileRef;
 	long index = 32;
 	long number;

 	// open the file, put the fileRef into sFileRef
 	sFileRef = open ("Profile", O_RDWR);
 	if (sFileRef == -1)
 			return (KCS_IO_ERROR);
 	// create a file object
 	file = new KcsFile(&sStatus, sFileRef, 0);
 	if (sStatus != KCS_SUCCESS)
 			return(sStatus);

 	// using the file object, read from the file into a buffer.
 	sStatus = file->absRead(index, sizeof(long), &number);
 	delete file;
 	close(sFileRef);

 	return (sStatus);
 }

Writing to a File From the Last Cursor Position

This example shows you how to write to a file with relWrite(). See Table 2-3 for a full definition of relWrite().


Example 2-4 Writing to a File From the Last Cursor Position

KcsStatus writeIt()
 {
 	KcsStatus sStatus;
 	KcsFileId sFileRef;
 	long nbytes;
 	char *buffer;

 	// open the file, get a fileRef
 	sFileRef = open ("Profile", O_RDWR);
 	if (sFileRef == -1)
 			return (KCS_IO_ERROR);

 	// create a file object
 	file = new KcsFile(&sStatus, sFileRef);
 	if (sStatus != KCS_SUCCESS)
 			return(sStatus);

 	// Allocate memory for the buffer, fill it with data.
 	// Set nbytes to the length of the buffer.
 	if ((buffer = (char*) malloc(nbytes)) == NULL)
 			return (KCS_IO_ERROR);
 	delete file;
 	close(sFileRef);

 	// using the file object, write the buffer to the file.
 	sStatus = file->relWrite(nbytes, buffer);

 	// Free the buffer's memory.
 	free (buffer);
 	delete file;
 	close(sFileRef);

 	return (sStatus);
 }