怎样用C语言列出一个文件的日期和时间?
在_dos_findirst()和_dos_findfnext()函数所返回的find_t结构中(请参见4.8),存放着查找到的文件的日期和时间,因此,只要对4.8中的例子稍作改动,就可以列出每个文件的日期、时间和文件名。
文件的日期和时间存放在结构成员find_t.wr_date和find_t.wr_time中。文件的时间存放在一个双字节的无符号整数中,见下表:
-------------------------------------------------------------
元 素 位域大小 取值范围
-------------------------------------------------------------
秒 5位 0—29(乘以2后为秒值)
分 6位 0—59
时 5位 0—23
-------------------------------------------------------------
文件的日期同样也存放在一个双字节的无符号整数中,见下表:
-------------------------------------------------------------
元 素 位域大小 取值范围
-------------------------------------------------------------
日 5位 1—31
月 4位 1—12
年 7位 1--127(加上1980后为年值)
-------------------------------------------------------------
因为DOS存储文件的秒数的间隔为两秒,所以只需使用0--29这个范围内的值。此外,DOS产生于1980年,因此文件的日期不可能早于1980年,你必须加上“1980”这个值才能得到真正的年值。
以下是列出某个目录下所有的文件及其日期和时间的一个例子:
#include <stdio.h>
#include <direct.h>
#include <dos. h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK
void main(void);
void main(void)
{
FILE_BLOCK f_block; /* Define the find-t structure variable * /
int ret-code; / * Define a variable to store return codes * /
int hour; / * We're going to use a 12-hour clockl * /
char * am_pm; / * Used to print "am" or "pm" * /
printf("\nDireetory listing of all files in this directory:\n\n");
/ * Use the ' *. * ' file mask and the 0xFF attribute mask to list
all files in the directory, including system files, hidden
files, and subdirectory names. * /
ret_code = _dos_findfirst(" *.* ", 0xFF, &f_block);
/* The_dos_findfirst() function returns a 0 when it is successful
and has found a valid filename in the directory. * /
while (ret_code == 0)
{
/ * Convert from a 24-hour format to a 12-hour format. * /
hour = (f_block. wr_time>>11);
if (hour > 12)
{
hour = hour - 12;
am_pm = "pm";
}
else
am_pm="am";
/ * Print the file's name, date stamp, and time stamp. * /
printf("%-12s %2d/%2d/%4d %2d:%2d:%02d %s\n",
f_block.name, / * name * /
(f-block.wr_date >> 5) & 0x0F, / * month * /
(f_block.wr_date) & 0x1F, / * day * /
(f_block.wr_date >> 9) + 1980 , / * year * /
hour, / * hour * /
(f-block. wr_time >> 5) & 0x3F, / * minute * /
(f_block. wr_time & 0x1F) * 2, / * seconds * /
am_pm);
/* Use the _ dos_findnext() function to look
for the next file in the directory. * /
ret_code = _dos_findnext (&f_block);
}
printf("\End of directory listing. \n" );
}
请注意,为了获得时间变量和日期变量的各个元素,要进行大量移位操作和位处理操作,如果你非常讨厌这些操作,你可以自己定义一个find_t这样的结构,并为C语言定义的find_t结构和你自己定义的结构创建一个共用体(请看下例),从而改进上例中的代码。
/ * This is the find_t structure as defined by ANSI C. * /
struct find_t
{
char reserved[21];
char attrib;
unsigned wr_time;
unsigned wr_date;
long size;
char name[13];
/ * This is a custom find_t structure where we
separate out the bits used for date and time. * /
struet my_find_t
{
char reserved[21];
char attrib;
unstgned seconds: 5;
unsigned minutes: 6;
unsigned hours: 5;
unsigned day: 5;
unstgned month: 4;
unsigned year: 7;
long size;
char name[13];
}
/* Now, create a union between these two strucures
so that we can more easily access the elements of
wr_date and wr_time. * /
union file_info
{
struct find_t ft;
struct my_find_t mft;
}
用上例中的自定义结构和共用体,你就可以象下例这样来抽取日期变量和时间变量的各个元素,而不必再进行移位操作和位处理操作了:
...
file_info my_file;
...
printf(" %-12s %2d/%2d/%4d %2d: %2d: %2d %s\n",
my_file, mfr.name, / * name * /
my-file, mfr.month, / * moth * /
my_file, mfr.day, / * day * /
(my-file. mft.year + 1980), / * year * /
my-file, raft. hours, / * hour * /
my- file. mfr. minutes, / * minute * /
(my_file. mft. seconds * 2), / * deconds * /
am_pm);
文件的日期和时间存放在结构成员find_t.wr_date和find_t.wr_time中。文件的时间存放在一个双字节的无符号整数中,见下表:
-------------------------------------------------------------
元 素 位域大小 取值范围
-------------------------------------------------------------
秒 5位 0—29(乘以2后为秒值)
分 6位 0—59
时 5位 0—23
-------------------------------------------------------------
文件的日期同样也存放在一个双字节的无符号整数中,见下表:
-------------------------------------------------------------
元 素 位域大小 取值范围
-------------------------------------------------------------
日 5位 1—31
月 4位 1—12
年 7位 1--127(加上1980后为年值)
-------------------------------------------------------------
因为DOS存储文件的秒数的间隔为两秒,所以只需使用0--29这个范围内的值。此外,DOS产生于1980年,因此文件的日期不可能早于1980年,你必须加上“1980”这个值才能得到真正的年值。
以下是列出某个目录下所有的文件及其日期和时间的一个例子:
#include <stdio.h>
#include <direct.h>
#include <dos. h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef struct find_t FILE_BLOCK
void main(void);
void main(void)
{
FILE_BLOCK f_block; /* Define the find-t structure variable * /
int ret-code; / * Define a variable to store return codes * /
int hour; / * We're going to use a 12-hour clockl * /
char * am_pm; / * Used to print "am" or "pm" * /
printf("\nDireetory listing of all files in this directory:\n\n");
/ * Use the ' *. * ' file mask and the 0xFF attribute mask to list
all files in the directory, including system files, hidden
files, and subdirectory names. * /
ret_code = _dos_findfirst(" *.* ", 0xFF, &f_block);
/* The_dos_findfirst() function returns a 0 when it is successful
and has found a valid filename in the directory. * /
while (ret_code == 0)
{
/ * Convert from a 24-hour format to a 12-hour format. * /
hour = (f_block. wr_time>>11);
if (hour > 12)
{
hour = hour - 12;
am_pm = "pm";
}
else
am_pm="am";
/ * Print the file's name, date stamp, and time stamp. * /
printf("%-12s %2d/%2d/%4d %2d:%2d:%02d %s\n",
f_block.name, / * name * /
(f-block.wr_date >> 5) & 0x0F, / * month * /
(f_block.wr_date) & 0x1F, / * day * /
(f_block.wr_date >> 9) + 1980 , / * year * /
hour, / * hour * /
(f-block. wr_time >> 5) & 0x3F, / * minute * /
(f_block. wr_time & 0x1F) * 2, / * seconds * /
am_pm);
/* Use the _ dos_findnext() function to look
for the next file in the directory. * /
ret_code = _dos_findnext (&f_block);
}
printf("\End of directory listing. \n" );
}
请注意,为了获得时间变量和日期变量的各个元素,要进行大量移位操作和位处理操作,如果你非常讨厌这些操作,你可以自己定义一个find_t这样的结构,并为C语言定义的find_t结构和你自己定义的结构创建一个共用体(请看下例),从而改进上例中的代码。
/ * This is the find_t structure as defined by ANSI C. * /
struct find_t
{
char reserved[21];
char attrib;
unsigned wr_time;
unsigned wr_date;
long size;
char name[13];
/ * This is a custom find_t structure where we
separate out the bits used for date and time. * /
struet my_find_t
{
char reserved[21];
char attrib;
unstgned seconds: 5;
unsigned minutes: 6;
unsigned hours: 5;
unsigned day: 5;
unstgned month: 4;
unsigned year: 7;
long size;
char name[13];
}
/* Now, create a union between these two strucures
so that we can more easily access the elements of
wr_date and wr_time. * /
union file_info
{
struct find_t ft;
struct my_find_t mft;
}
用上例中的自定义结构和共用体,你就可以象下例这样来抽取日期变量和时间变量的各个元素,而不必再进行移位操作和位处理操作了:
...
file_info my_file;
...
printf(" %-12s %2d/%2d/%4d %2d: %2d: %2d %s\n",
my_file, mfr.name, / * name * /
my-file, mfr.month, / * moth * /
my_file, mfr.day, / * day * /
(my-file. mft.year + 1980), / * year * /
my-file, raft. hours, / * hour * /
my- file. mfr. minutes, / * minute * /
(my_file. mft. seconds * 2), / * deconds * /
am_pm);