/* 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 */ /* datatype-specific 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 #include #include "gfcursor.hpp" int main(void) { int valid; short i; char ch; char *strptr; size_t strsize; CURSOR *c = new CURSOR("newfile"); c->eCursorQueryValid(&valid); if (!valid) exit(-1); c->eCursorMoveIn(); c->eObjRead(&i); c->eCursorMoveNext(); c->eObjRead(&ch); c->eCursorMoveNext(); c->eObjQueryDataSize(&strsize); strptr = new char[strsize]; c->eObjRead(strptr, strsize); cout << "i = " << i << endl; cout << "ch = " << ch << endl; cout << "strptr = " << strptr << endl; delete (strptr); delete 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 pre-allocated */ /* block of memory. The size of the block of memory */ /* is then passed to the ObjRead function. */