/* Gamelon(tm) File I/O Library Sample Code  C Language   */

/* (C)1994 Menai Corporation(tm)  Released for public use */

 

/* Example 9a. How to read the contents of stored objects */

/* when structure of the file is known.                   */

 

/* When the program knows the structure of the file, the  */

/* program may simply move from object to object within   */

/* the file, reading the contents of each object using    */

/* datatypespecific read calls. For example, if the      */

/* contents of a particular file (or of an aggregate      */

/* within a file) are an integer value 14, a character    */

/* value 'F' and a string value "Wherefore art thou,      */

/* Romeo?", the function calls to read such a file could  */

/* be as follows:                                         */

 

/* Beginning file layout:                     */

/*  {                                       } */

/*   [14]['F']["Wherefore art thou, Romeo?"]  */

 

#include <stdlib.h>

#include <stdio.h>

#include "gfcursor.h"

 

int main(void) {

int valid;

short i;

char ch;

char *strptr;

size_t strsize;

 

CURSOR *c = new_CURSOR(NULL,

"newfile",

AM_SHARED,

NULL,

0);

eCursorQueryValid(c, &valid);

if (!valid)

exit(1);

eCursorMoveIn(c);

eObjReadShort(c, &i);

eCursorMoveNext(c, OT_OBJ);

eObjReadChar(c, &ch);

eCursorMoveNext(c, OT_OBJ);

eObjQueryDataSize(c, &strsize);

strptr = malloc(strsize);

eObjReadString(c, strptr, strsize);

printf("i = %hd\n", i);

printf("ch = %c\n", ch);

printf("strptr = %s\n", strptr);

free(strptr);

delete_CURSOR(c);

return 0;

}

 

/* Program output:                     */

/* i = 14                              */

/* ch = F                              */

/* strptr = Wherefore art thou, Romeo? */

 

/* The ObjRead functions are overloaded; as a result, */

/* all the ObjRead calls look similar. The parameters */

/* of each function call distinguish the exact        */

/* function called to read information from the       */

/* object. Note that in order to properly read the    */

/* object containing the string value, the programmer */

/* must first query the object for its datasize, in   */

/* order to allocate enough memory for the string, or */

/* the programmer can just use a large preallocated  */

/* block of memory. The size of the block of memory   */

/* is then passed to the ObjRead function.            */

Download this sample source code

Return to Samples List

 Home | Product | Consulting | Programming | Reviews | Company | Site Map | Guest Book

 Menai Corporation, 1010 El Camino Real, Suite 300, Menlo Park, California 94025-4335
 800-GAMELON (800-426-3566) (US only), 650-853-6450 (voice), 650-853-6453 (fax)
 Questions about our products and services:
info@menai.com.
 Comments about this web site:
webmaster@menai.com.

Copyright © 1996-98 Menai Corporation. All Rights Reserved. Menai, Gamelon and gamelon(stylized) are worldwide trademarks of Menai Corporation, registered in the United States of America, and the .[g] logo is a worldwide trademark of Menai Corporation. All other trademarks are owned by their respective owners.