home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1706.ZIP / TI1706.ASC
Text File  |  1993-10-12  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1706
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 12, 1993                         PAGE  :  1/2
  12.  
  13.     TITLE  :  Reading the time and date bitfields for a file.
  14.  
  15.  
  16.  
  17.  
  18.   Many users need to retrieve the time and date of a file, but
  19.   don't know how to use bitfields. The following example gets the
  20.   time and date using the _dos_getftime() function.
  21.  
  22.   _dos_getftime() returns the date and time in two unsigned
  23.   integers.  Each unsigned integer is laid out as a bitfield. In
  24.   the example the two structures Time and Date are defined as
  25.   bitfields that match the layout of the values returned by
  26.   _dos_getftime.
  27.  
  28.   By passing the two structures to _dos_getftime, they will get
  29.   filled in. Then you will be able to retrieve the day, month, and
  30.   other fields as individual structure variables.
  31.  
  32.   Two things to watch out for are the year and seconds. The year
  33.   is stored as number of years since 1980, so add 1980 to the value
  34.   of the field. The seconds are divided by 2 so multiple by 2 to
  35.   get the correct value. These were done in order to fit all the
  36.   fields into 16 bits.
  37.  
  38.   The example below opens CONFIG.SYS and gets the time and
  39.   date it was last modified. Make note the bitfields for each
  40.   structure add up to sixteen, the size of an unsigned int. If you
  41.   don't do the same you could have alignment problems, so count
  42.   your bits. The setw() and setfill() in the cout were added for
  43.   the leading zero if minutes is a single digit value. This is
  44.   just for cosmetic reasons. This example should also give you a
  45.   good handle on accessing bitfields in general. Bitfields are a
  46.   valuable tool for saving space.
  47.  
  48.   #include <iostream.h>
  49.   #include <iomanip.h>
  50.   #include <dos.h>
  51.   #include <fcntl.h>
  52.  
  53.   void main ()
  54.   {
  55.     struct Time {
  56.       unsigned seconds:5; // Seconds are divded by 2 to save space
  57.       unsigned minutes:6;
  58.       unsigned hours:5;
  59.     } time;
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1706
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 12, 1993                         PAGE  :  2/2
  78.  
  79.     TITLE  :  Reading the time and date bitfields for a file.
  80.  
  81.  
  82.  
  83.  
  84.     struct Date {
  85.       unsigned int day:5;
  86.       unsigned int month:4;
  87.       unsigned int year:7; // The year is store as number of years
  88.     } d8;                  // since 1980
  89.  
  90.     int hndl;
  91.  
  92.     if (_dos_open("\\config.sys", O_RDONLY, &hndl))
  93.       cout << "Error open \CONFIG.SYS" << endl;
  94.     else
  95.     {
  96.       if (_dos_getftime(hndl,(unsigned *)&d8, (unsigned *)&time))
  97.          cout << "Error getting CONFIG.SYS's date and time\n";
  98.       else
  99.       {
  100.          cout << "CONFIG.SYS was last modified on: "
  101.                   << d8.month << "/"
  102.                   << d8.day   << "/"
  103.                   << d8.year+1980 << " at "
  104.                   << time.hours << ":" << setw(2) << setfill('0')
  105.                   << time.minutes << ":"
  106.                   << time.seconds * 2 << endl;
  107.       }
  108.       _dos_close(hndl);
  109.     }
  110.   }
  111.  
  112.   DISCLAIMER: You have the right to use this technical information
  113.   subject to the terms of the No-Nonsense License Statement that
  114.   you received with the Borland product to which this information
  115.   pertains.
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.