home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / msdos / programm / 12400 < prev    next >
Encoding:
Text File  |  1993-01-24  |  4.6 KB  |  173 lines

  1. Path: sparky!uunet!ukma!usenet.ins.cwru.edu!agate!darkstar.UCSC.EDU!cats.ucsc.edu!spencer
  2. From: spencer@cats.ucsc.edu (Michael Spencer)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: Random access files under TC++???
  5. Date: 24 Jan 1993 18:24:11 GMT
  6. Organization: University of California, Santa Cruz
  7. Lines: 161
  8. Message-ID: <1jumsbINNft5@darkstar.UCSC.EDU>
  9. References: <1993Jan20.185339.19456@chamber.in-berlin.de>
  10. NNTP-Posting-Host: am.ucsc.edu
  11.  
  12.  
  13.  
  14. hausutzu@chamber.in-berlin.de (Utz-Uwe Haus) writes:
  15.  
  16. >Hello,
  17. >I'm currently writing a small database for a friend's company and encountered
  18. >the following problem: 
  19. >1) I save the records in a fstream file, build 2 indexes for easier search
  20. >(everything okay so far)
  21. >2) When I try to change the content of a record (read it, change the 
  22. >appropriate data and try to write it back, it gets appended to the end of the 
  23. >file, although I opened the file ios::out (I also tried ios::out|ios::app and a
  24. >seekg to the required position and ios::out|ios::ate and seekg()). In addition
  25. >to the wrong position for the rewrite the file sometimes is corrupted 
  26. >afterwards, making read-access for my build_index() - function impossible.
  27.  
  28.  
  29. >Utz-Uwe Haus
  30. >root%pse@chamber.in-berlin.de
  31.  
  32. Can't help you if you insist on using C++ io streams.
  33. Generally I try to stay away from them, because they have proven
  34. conceptually unstable in the past (Stroustrup keeps changing his
  35. mind).
  36.  
  37. If you don't mind using old-fashioned C i/o, the following has been
  38. used successfully in just the kind of application you are 
  39. describing.
  40.  
  41.  
  42. ===========================================================================
  43. Michael Spencer            |      2640 Borregas
  44. spencer@cats.ucsc.edu      |      Aptos, CA   95003
  45. ===========================================================================
  46.  
  47.                                                             /* 04-16-1989 */
  48.  
  49. #define                         FILES_C
  50.  
  51.  
  52. #include <stdio.h>
  53. #include <fcntl.h>
  54. #include <dir.h>
  55. #include <cc_std.h>                               /* my basic #defines */
  56.  
  57. bool DataMake   (int *fd, char *fname);           /* pass fd as DM (&fd,..) */
  58. bool DataOpen   (int *fd, char *fname);
  59. bool DataAppend (int *fd, char *fname);
  60. void DataClose  (int fd);
  61.  
  62. long DataFileSize (int fd, int recSize);         /* num records in file */
  63. bool DataGet (int fd, long recNum, void *buf, int recSize);
  64. bool DataPut (int fd, long recNum, void *buf, int recSize);
  65.  
  66.  
  67. /* ----------------------------------- DATA */
  68.  
  69. #define S_IREAD                 256        /* read permission, owner */
  70. #define S_IWRITE                128        /* write permission, owner */
  71. #define S_RDWR                  (S_IREAD | S_IWRITE)
  72.  
  73. #define S_MAKE                  (O_CREAT  | O_TRUNC | O_RDWR | O_BINARY)
  74. #define S_OPEN                  (O_RDWR   | O_BINARY)
  75. #define S_APPEND                (O_APPEND | O_WRONLY)
  76.  
  77.  
  78.  
  79. bool DataMake (int *fd, char *fname)
  80.      {
  81.      bool           bad;
  82.  
  83.      errno = 0;
  84.      *fd = open (fname, S_MAKE, S_RDWR);
  85.      bad = (*fd == -1);
  86.      if (bad)  Errio (fname);
  87.      errno = 0;
  88.      return (! bad);
  89.      }
  90.  
  91.  
  92.  
  93. bool DataOpen (int *fd, char *fname)
  94.      {
  95.      bool           bad;
  96.  
  97.      errno = 0;
  98.      *fd = open (fname, S_OPEN);
  99.      bad = (*fd == -1);
  100.      if (bad)  Errio (fname);
  101.      errno = 0;
  102.      return (! bad);
  103.      }
  104.  
  105.  
  106.  
  107. bool DataAppend (int *fd, char *fname)
  108.      {
  109.      bool           bad;
  110.  
  111.      errno = 0;
  112.      *fd = open (fname, S_APPEND);
  113.      bad = (*fd == -1);
  114.      if (bad)  Errio (fname);
  115.      errno = 0;
  116.      return (! bad);
  117.      }
  118.  
  119.  
  120.  
  121. void DataClose (int fd)
  122.      {
  123.      close (fd);
  124.      errno = 0;
  125.      }
  126.  
  127.  
  128.  
  129. long DataFileSize (int fd, int recSize)
  130.      {
  131.      long      size;
  132.  
  133.      size = filelength (fd);
  134.      if (Errio (""))
  135.           size = 0L;
  136.        else size /= recSize;
  137.      return size;
  138.      }
  139.  
  140.  
  141.  
  142. bool DataGet (int fd, long recNum, void *buf, int recSize)
  143.      {
  144.      long      kk;
  145.  
  146.      kk = DataFileSize (fd, recSize) - 1;
  147.      if (Errata (! in (recNum, 0, kk), badRec))  return no;
  148.      lseek (fd, recNum * recSize, 0);
  149.      return (read (fd, buf, recSize) == recSize);
  150.      }
  151.  
  152.  
  153.  
  154. bool DataPut (int fd, long recNum, void *buf, int recSize)
  155.      {
  156.      long      kk;
  157.  
  158.      kk = DataFileSize (fd, recSize);
  159.      if (Errata (! in (recNum, 0, kk), badRec))  return no;
  160.      lseek (fd, recNum * recSize, 0);
  161.      return (write (fd, buf, recSize) == recSize);
  162.      }
  163.  
  164.  
  165.                                                                       
  166. -- 
  167.  
  168. ===========================================================================
  169. Michael Spencer            |      2640 Borregas
  170. spencer@cats.ucsc.edu      |      Aptos, CA   95003
  171. ===========================================================================
  172.  
  173.