home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / rfile.c < prev    next >
Text File  |  1985-09-28  |  2KB  |  79 lines

  1. /*   These functions are for Random File Access and are written in
  2. *    Lattice `C'.  The file "ALL.H" contains the following
  3. *    definitions:    O_RDWR = 2
  4. *                   O_CREAT = 0x0100
  5. *                     O_RAW = 0x8000
  6. *
  7. *    When a file is open [ropen()] YOU MUST KEEP TRACK OF THE HI RECORD
  8. *    NUMBER (endr).
  9. */
  10.  
  11. /*   RFILE.C  Random file access functions  */
  12.  
  13. #include "ALL.H"
  14.  
  15. int ropen(filspc)   /*  open file for random access  */
  16.  
  17. char *filspc;
  18.  
  19. { int port;
  20.  
  21.   port=open(filspc,O_RDWR | O_CREAT | O_RAW);
  22.   return(port);  /*  port<0 = error  */
  23. }
  24.  
  25.  
  26. int rfile(port,rec,data,sz,mode)     /*  random file access         */
  27.                                   /*   assumes port assigned     */
  28. int port,rec;
  29. int sz;          /*  sizeof(*data)  */
  30. char *data;      /*  random record structure  */
  31. char mode;       /*  r = read, w = write  */
  32.  
  33. { int stat,read(),write();
  34.   long pos,lseek();
  35.  
  36.   stat=0;
  37.   rec-=1;
  38.   pos=rec*sz;
  39.   lseek(port,pos,0);
  40.   switch(mode)   {  case'r':stat=read(port,data,sz);
  41.                             break;
  42.                     case'w':stat=write(port,data,sz);
  43.                             break;
  44.                  };
  45.   if(stat<=0) return(stat-1);   /* -1=EOF, <(-1)=error */
  46.   else return(rec+1);
  47. }
  48.  
  49.  
  50. int hirec(port,sz)   /*  random record count  */
  51.  
  52. int port;
  53. int sz;       /*  sizeof record  */
  54.  
  55. { int ct;
  56.   long ef,lseek();
  57.  
  58.   ef=lseek(port,0L,2);
  59.   ef+=1;
  60.   ct=ef/sz;
  61.   return(ct);
  62. }
  63.  
  64.  
  65. int rclose(port,sz,endr)    /*  random file close  */
  66.  
  67. int port,endr;      /*  file pointer & end-record  */
  68. int sz;             /*  size of record  */
  69.  
  70. { long pos,er,lseek();
  71.   int r;
  72.  
  73.   pos=sz*endr+1;
  74.   er=lseek(port,pos,0);
  75.   close(port);
  76.   r=er;
  77.   return(r);      /*  err<0 = error  */
  78. }
  79.