home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / A < prev    next >
Encoding:
Text File  |  1992-04-01  |  1.8 KB  |  72 lines

  1. /*
  2.  * Example 7: FileManager.  Saves a linked list of ints.
  3.  * This example appears in Section 11.2 of the manual.
  4.  *
  5.  * $Header:   E:/vcs/toolexam/example7.cpv   1.1   01 Apr 1992 16:51:12   keffer  $
  6.  *
  7.  ****************************************************************************
  8.  *
  9.  * Rogue Wave Software, Inc.
  10.  * P.O. Box 2328
  11.  * Corvallis, OR 97339
  12.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  13.  *
  14.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  15.  * protection under the laws of the United States and other countries.
  16.  *
  17.  ***************************************************************************
  18.  *
  19.  * $Log:   E:/vcs/toolexam/example7.cpv  $
  20.  * 
  21.  *    Rev 1.1   01 Apr 1992 16:51:12   keffer
  22.  * Now includes <rw/xxx.h>
  23.  * 
  24.  */
  25.  
  26. #include <rw/filemgr.h>
  27. #include <rw/rstream.h>
  28.  
  29. struct DiskNode {
  30.   int      data;
  31.   RWoffset nextNode;
  32. };
  33.  
  34. main()
  35. {
  36.   cout << "Test of a linked-list on disk, using the RWFileManager.\n";
  37.  
  38.   RWFileManager fm("linklist.dat");
  39.   
  40.   // Allocate space for offset to start of the linked list:
  41.   fm.allocate(sizeof(RWoffset));
  42.   // Allocate space for the first link:
  43.   RWoffset thisNode = fm.allocate(sizeof(DiskNode));
  44.   
  45.   fm.SeekTo(fm.start());
  46.   fm.Write(thisNode);
  47.   
  48.   DiskNode n;
  49.   int temp;
  50.   RWoffset lastNode;
  51.   cout << "Input a series of integers, then EOF to end:\n";
  52.  
  53.   /* Borland bug necessitates explicit test for good. */  
  54.   while ( (cin >> temp).good() ){
  55.     n.data = temp;
  56.     n.nextNode = fm.allocate(sizeof(DiskNode));
  57.     fm.SeekTo(thisNode);
  58.     fm.Write(n.data);
  59.     fm.Write(n.nextNode);
  60.     lastNode = thisNode;
  61.     thisNode = n.nextNode;
  62.   }
  63.   
  64.   fm.deallocate(n.nextNode);
  65.   n.nextNode = NIL;
  66.   fm.SeekTo(lastNode);
  67.   fm.Write(n.data);    
  68.   fm.Write(n.nextNode);
  69.   cout << "Now run example8 to read them in.\n";
  70.   return 0;
  71. }
  72.