home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / filutl / fileio.arc / FILEIO.C
Text File  |  1988-07-27  |  3KB  |  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. /*  get_rec()  */
  31. /*  this function gets a specific record from a random file. The */
  32. /*  required arguments are the file number, file length, relative record */
  33. /*  number, and a pointer to the input data structure.  */
  34. /*  It returns 0 if successful, or 1 if unsuccessful (read past EOF) */
  35.  
  36. int get_rec(filenum, filelgth, recno, structur)
  37. int filenum, filelgth, recno;
  38. char  structur[];
  39. {
  40.      int stats;
  41.      long offset, rtrn, lseek();
  42.  
  43.      offset = (long) (recno-1) * filelgth;
  44.      rtrn = lseek(filenum, offset, 0);           
  45.      stats = read(filenum, structur, filelgth);
  46.      if (stats > 0l) {
  47.           return(0);
  48.      }    else {
  49.                return(1);
  50.           }
  51. }
  52.  
  53. /*  wrt_rec()  */
  54. /*  this function writes a specific record to a random file. The */
  55. /*  required arguments are the file number, file length, relative record */
  56. /*  number, and a pointer to the input data structure.  */
  57. /*  It returns 0 if successful, or 1 if unsuccessful */
  58.  
  59. int wrt_rec(filenum, filelgth, recno, structur)
  60. int filenum, filelgth, recno;
  61. char structur[];
  62. {
  63.      int stats, c;
  64.      long offset, rtrn, lseek();
  65.  
  66.      offset = (long) (recno-1) * filelgth;
  67.      rtrn = lseek(filenum, offset, 0);
  68.      stats = write(filenum, structur, filelgth);
  69.      if (stats > 0l) {
  70.           return(0);
  71.      } else {
  72.           return(1);
  73.      }
  74. }
  75.  
  76. /*  wrt_add()  */
  77. /*  this function appends a record to the end of a sequential file. The  */
  78. /*  required arguments are the file number, file length, and a pointer  */
  79. /*  to the input data structure.  */
  80. /*  It returns 0 if successful, or 1 if unsuccessful */
  81.  
  82. int wrt_add(filenum, filelgth, structur)
  83. int filenum, filelgth;
  84. char structur[];
  85. {
  86.      int stats, c;
  87.      long rtrn, lseek();
  88.  
  89.      rtrn = lseek(filenum, 0L, 2);     
  90.      stats = write(filenum, structur, filelgth);
  91.      if (stats > 0l) {
  92.           return(0);
  93.      } else {
  94.           return(1);
  95.      }
  96. }
  97.