home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12107a < prev    next >
Text File  |  1991-10-30  |  2KB  |  46 lines

  1. //  Listing 6 - Functions to write records 
  2. //  (See Listing 3 for the class definition)
  3.  
  4. //  =============================================
  5. //  For a file opened in Append or Recreate modes this
  6. //      appends new records to the end of the file.
  7. //  For a file opened in Update or AnyWrite modes this
  8. //    rewrites the current record.
  9. //
  10. int binaryfile::filewrite () { 
  11.     int ret;
  12.     if (mode == Readonly) return -2;  // Error trap
  13.     if (mode == Update || mode == AnyWrite) {
  14.        return filewrite (recnbr); // rewrite current record
  15.     }
  16.     if ((ret = write (handle, record, length)) != -1) {
  17.        recnbr = (share == WriteShared ? countrecords() : (count += 1L));
  18.     }
  19.     return ret;
  20. }
  21.  
  22. //  =============================================
  23. //  Function to write out a specific record in class
  24. //      binaryfile.  This function can only be used if
  25. //      the file was opened in Update or AnyWrite modes.
  26. //  To append to a file (AnyWrite mode only) use any 
  27. //      very large record number
  28. //
  29. int binaryfile::filewrite (long number) { 
  30.     int ret;
  31.     if ((mode != Update && mode != AnyWrite) || 
  32.     number <= 0 ||
  33.         (mode == Update && number > count)) 
  34.     return -2;  // Parameter error trap
  35.     if (number > count) number = countrecords() + 1;
  36.     if (number != locked && share == WriteShared) {
  37.        if (lockrecord (number)) return -10;
  38.     }
  39.     lseek (handle, ((number - 1) * length) + header, SEEK_SET);
  40.     if ((ret = write (handle, record, length)) != -1L &&
  41.        number > count) count += 1L; // new record at end of file
  42.     unlockrecord ();
  43.     recnbr = number;
  44.     return ret;
  45. }
  46.