home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / lattice / fileio.c < prev    next >
Encoding:
Text File  |  1988-07-27  |  3.3 KB  |  98 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* This function was written by Jim Niederriter for the Lattice `C' Compiler*/
  4. /* on the IBM Personal Computer or compatible, running DOS 2.0.             */
  5. /*                                                                          */
  6. /****************************************************************************/
  7. /*  fileio.c  */
  8.  
  9. /*  This file is a group of several useful I/O functions */
  10.  
  11. /*  open_fil()  */
  12. /*  this function opens a disk file in untranslated mode. The arguments */
  13. /*  required are the filename pointer, and the mode of operation.  */
  14. /*  It returns the file number.  */
  15. /*  Valid modes are: 0=read, 1=write, 2=read/write  */
  16. /*  NOTE: Untranslated mode is required for binary files, but not for */
  17. /*  ASCII files. Untranslated is specified by turning on bit 15 of    */
  18. /*  the MODE value.  */
  19.  
  20. int open_fil(filename, mode)
  21. char filename[];
  22. int mode;
  23. {
  24.      int f1;                            /* declare file numberint */
  25.      mode |= 0x8000;                    /* set on bit 15 of mode  */
  26.      f1 = open(filename, mode);
  27.      return(f1);                        /* return file number  */
  28. }
  29. รจ
  30.  
  31.  
  32. /*  get_rec()  */
  33. /*  this function gets a specific record from a random file. The */
  34. /*  required arguments are the file number, file length, relative record */
  35. /*  number, and a pointer to the input data structure.  */
  36. /*  It returns 0 if successful, or 1 if unsuccessful (read past EOF) */
  37.  
  38. int get_rec(filenum, filelgth, recno, structur)
  39. int filenum, filelgth, recno;
  40. char  structur[];
  41. {
  42.      int stats;
  43.      long offset, rtrn, lseek();
  44.  
  45.      offset = (long) (recno-1) * filelgth;
  46.      rtrn = lseek(filenum, offset, 0);           
  47.      stats = read(filenum, structur, filelgth);
  48.      if (stats > 0l) {
  49.           return(0);
  50.      }    else {
  51.                return(1);
  52.           }
  53. }
  54.  
  55. /*  wrt_rec()  */
  56. /*  this function writes a specific record to a random file. The */
  57. /*  required arguments are the file number, file length, relative record */
  58. /*  number, and a pointer to the input data structure.  */
  59. /*  It returns 0 if successful, or 1 if unsuccessful */
  60.  
  61. int wrt_rec(filenum, filelgth, recno, structur)
  62. int filenum, filelgth, recno;
  63. char structur[];
  64. {
  65.      int stats, c;
  66.      long offset, rtrn, lseek();
  67.  
  68.      offset = (long) (recno-1) * filelgth;
  69.      rtrn = lseek(filenum, offset, 0);
  70.      stats = write(filenum, structur, filelgth);
  71.      if (stats > 0l) {
  72.           return(0);
  73.      } else {
  74.           return(1);
  75.      }
  76. }
  77.  
  78. /*  wrt_add()  */
  79. /*  this function appends a record to the end of a sequential file. The  */
  80. /*  required arguments are the file number, file length, and a pointer  */
  81. /*  to the input data structure.  */
  82. /*  It returns 0 if successful, or 1 if unsuccessful */
  83.  
  84. int wrt_add(filenum, filelgth, structur)
  85. int filenum, filelgth;
  86. char structur[];
  87. {
  88.      int stats, c;
  89.      long rtrn, lseek();
  90.  
  91.      rtrn = lseek(filenum, 0L, 2);     
  92.      stats = write(filenum, structur, filelgth);
  93.      if (stats > 0l) {
  94.           return(0);
  95.      } else {
  96.           return(1);
  97.      }
  98. }
  99.