home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DOS.ZIP / RFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-03-30  |  1.9 KB  |  81 lines

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