home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
High Voltage Shareware
/
high1.zip
/
high1
/
DIR13
/
TI_BC1.ZIP
/
TI1706.ZIP
/
TI1706.ASC
Wrap
Text File
|
1993-10-12
|
3KB
|
133 lines
PRODUCT : Borland C++ NUMBER : 1706
VERSION : All
OS : All
DATE : October 12, 1993 PAGE : 1/2
TITLE : Reading the time and date bitfields for a file.
Many users need to retrieve the time and date of a file, but
don't know how to use bitfields. The following example gets the
time and date using the _dos_getftime() function.
_dos_getftime() returns the date and time in two unsigned
integers. Each unsigned integer is laid out as a bitfield. In
the example the two structures Time and Date are defined as
bitfields that match the layout of the values returned by
_dos_getftime.
By passing the two structures to _dos_getftime, they will get
filled in. Then you will be able to retrieve the day, month, and
other fields as individual structure variables.
Two things to watch out for are the year and seconds. The year
is stored as number of years since 1980, so add 1980 to the value
of the field. The seconds are divided by 2 so multiple by 2 to
get the correct value. These were done in order to fit all the
fields into 16 bits.
The example below opens CONFIG.SYS and gets the time and
date it was last modified. Make note the bitfields for each
structure add up to sixteen, the size of an unsigned int. If you
don't do the same you could have alignment problems, so count
your bits. The setw() and setfill() in the cout were added for
the leading zero if minutes is a single digit value. This is
just for cosmetic reasons. This example should also give you a
good handle on accessing bitfields in general. Bitfields are a
valuable tool for saving space.
#include <iostream.h>
#include <iomanip.h>
#include <dos.h>
#include <fcntl.h>
void main ()
{
struct Time {
unsigned seconds:5; // Seconds are divded by 2 to save space
unsigned minutes:6;
unsigned hours:5;
} time;
PRODUCT : Borland C++ NUMBER : 1706
VERSION : All
OS : All
DATE : October 12, 1993 PAGE : 2/2
TITLE : Reading the time and date bitfields for a file.
struct Date {
unsigned int day:5;
unsigned int month:4;
unsigned int year:7; // The year is store as number of years
} d8; // since 1980
int hndl;
if (_dos_open("\\config.sys", O_RDONLY, &hndl))
cout << "Error open \CONFIG.SYS" << endl;
else
{
if (_dos_getftime(hndl,(unsigned *)&d8, (unsigned *)&time))
cout << "Error getting CONFIG.SYS's date and time\n";
else
{
cout << "CONFIG.SYS was last modified on: "
<< d8.month << "/"
<< d8.day << "/"
<< d8.year+1980 << " at "
<< time.hours << ":" << setw(2) << setfill('0')
<< time.minutes << ":"
<< time.seconds * 2 << endl;
}
_dos_close(hndl);
}
}
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.