📄 chap14.lst
字号:
listing 1
struct tm {
int tm_sec; /* seconds, 0-59 */
int tm_min; /* minutes, 0-59 */
int tm_hour; /* hours, 0-23 */
int tm_mday; /* day of the month, 1-31 */
int tm_mon; /* months since Jan, 0-11 */
int tm_year; /* years from 1900 */
int tm_wday; /* days since Sunday, 0-6 */
int tm_yday; /* days since Jan 1, 0-365 */
int tm_isdst; /* daylight saving time indicator */
};
listing 2
struct date {
int da_year; /* year */
char da_day; /* day of month */
char da_mon; /* month, Jan=1 */
};
struct time {
unsigned char ti_min; /* minutes */
unsigned char ti_hour; /* hours */
unsigned char ti_hund; /* hundredths of seconds */
unsigned char ti_sec; /* seconds */
};
listing 3
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf(asctime(ptr));
return 0;
}
listing 4
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start, stop;
unsigned long t;
start = clock();
for(t=0; t<500000L; t++);
stop = clock();
printf("Loop required %f seconds",
(stop - start) / CLK_TCK);
return 0;
}
listing 5
asctime(localtime(time))
listing 6
#include <stdio.h>
#include <time.h>
#include <stddef.h>
int main(void)
{
time_t lt;
lt = time(NULL);
printf(ctime(<));
return 0;
}
listing 7
#include <stdio.h>
#include <time.h>
#include <stddef.h>
int main(void)
{
time_t start,end;
long unsigned int t;
start = time(NULL);
for(t=0; t<500000L; t++) ;
end = time(NULL);
printf("Loop required %f seconds", difftime(end, start));
return 0;
}
listing 8
_dos_close(fd);
listing 9
int fd;
if(_dos_creat("test.tst", _A_NORMAL, &fd))
printf("Cannot open file.\n");
listing 10
struct dosdate_t {
unsigned char day;
unsigned char month;
unsigned int year;
unsigned char dayofweek; /* Sunday is 0 */
};
listing 11
struct dostime_t {
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char hsecond; /* hundredths of second */
};
listing 12
#include <stdio.h>
#include <dos.h>
int main(void)
{
struct dosdate_t d;
struct dostime_t t;
_dos_getdate(&d);
_dos_gettime(&t);
printf("Time and date: %d:%d:%d, %d/%d/%d",
t.hour, t.minute, t.second, d.month, d.day,
d.year);
return 0;
}
listing 13
struct diskfree_t {
unsigned total_clusters;
unsigned avail_clusters;
unsigned sectors_per_cluster;
unsigned bytes_per_sector;
};
listing 14
#include <dos.h>
#include <stdio.h>
int main(void)
{
struct diskfree_t p;
_dos_getdiskfree(3, &p); /* drive C */
printf("Number of free clusters is %d.",
p.avail_clusters);
return 0;
}
listing 15
unsigned d;
_dos_getdrive(&d);
printf("drive is %c", d-1+'A');
listing 16
unsigned attr;
if(_dos_getfileattr("test.tst", &attr))
printf("file error");
if(attr & _A_NORMAL) printf("File is normal.\n");
listing 17
#include <io.h>
#include <dos.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct {
unsigned day: 5;
unsigned month: 4;
unsigned year: 7;
} d;
unsigned t;
int fd;
if(_dos_open("TEST.TST", O_RDONLY, &fd)) {
printf("Cannot open file.\n");
exit(1);
}
_dos_getftime(fd, (unsigned *) &d, &t);
printf("Date of creation: %u", d.year+1980);
return 0;
}
listing 18
int fd;
if(_dos_open("test.tst", O_RDWR, &fd))
printf("Error opening file.");
listing 19
int fd;
unsigned count
char *buf[128];
.
.
.
if(_dos_read(fd, buf, 128, &count))
printf("Error reading file.\n");
listing 20
struct dosdate_t {
unsigned char day;
unsigned char month;
unsigned int year;
unsigned char dayofweek; /* Sunday is 0 */
};
listing 21
struct dostime_t {
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char hsecond; /* hundredths of second */
};
listing 22
struct dostime_t t;
t.hour = 10;
t.minute = 10;
t.second = 10;
t.hsecond = 0;
_dos_settime(&t);
listing 23
unsigned num;
_dos_setdrive(3, &num);
listing 24
unsigned attr;
attr = _A_RDONLY;
if(_dos_setfileattr("test.tst", attr))
printf("File Error");
listing 25
#include <stdio.h>
#include <io.h>
#include <dos.h>
#include <fcntl.h>
#include <stdlib.h>
int main(void)
{
struct dt {
unsigned day: 5;
unsigned month: 4;
unsigned year: 7;
} ;
union {
struct dt date_time;
unsigned u;
} d;
unsigned t;
int fd;
if(_dos_open("TEST.TST", O_RDWR, &fd)) {
printf("Cannot open file.\n");
exit(1);
}
_dos_getftime(fd, &d.u, &t);
d.date_time.year = 22;
_dos_setftime(fd, d.u, t);
return 0;
}
listing 26
int fd;
unsigned count
char *buf[128];
.
.
.
if(_dos_write(fd, buf, 128, &count))
printf("Error writing file.");
listing 27
struct timeb {
long time; /* time in seconds from Jan. 1, 1970 */
short millitm; /* milliseconds */
short timezone; /* difference between GMT and local time */
short dstflag; /* non-0 if daylight saving time is in effect */
};
listing 28
#include <stdio.h>
#include <sys\timeb.h>
int main(void)
{
struct timeb lt;
ftime(<);
printf("%ld seconds %d milliseconds.",lt.time,lt.millitm);
return 0;
}
listing 29
#include <stdio.h>
#include <time.h>
#include <dos.h>
int main(void)
{
time_t t;
struct time dos_time;
struct date dos_date;
struct tm *local;
getdate(&dos_date);
gettime(&dos_time);
t = dostounix(&dos_date, &dos_time);
local = localtime(&t);
printf("time and date: %s", asctime(local));
return 0;
}
listing 30
struct dfree {
unsigned df_avail; /* unused clusters */
unsigned df_total; /* total number of clusters */
unsigned df_bsec; /* number of bytes per sector */
unsigned df_sclus; /* number of sectors per cluster */
};
listing 31
#include <stdio.h>
#include <dos.h>
int main(void)
{
struct dfree p;
getdfree(3, &p); /* drive C */
printf("Number of free clusters is %d.", p.df_avail);
return 0;
}
listing 32
struct ftime {
unsigned ft_tsec: 5; /* seconds */
unsigned ft_min: 6; /* minutes */
unsigned ft_hour: 5; /* hours */
unsigned ft_day: 5; /* days */
unsigned ft_month: 4; /* month */
unsigned ft_year: 7; /* year from 1980 */
};
listing 33
#include <stdio.h>
#include <io.h>
#include <dos.h>
#include <fcntl.h>
#include <stdlib.h>
int main(void)
{
struct ftime p;
int fd;
if((fd=open("TEST.TST", O_RDONLY))==-1) {
printf("Cannot open file.\n");
exit(1);
}
getftime(fd, &p);
printf("%d", p.ft_year + 1980);
return 0;
}
listing 34
#include <stdio.h>
#include <time.h>
#include <stddef.h>
/* print local and GM time */
int main(void)
{
struct tm *local, *gm;
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local time and date: %s", asctime(local));
gm = gmtime(&t);
printf("Greenwich mean time and date: %s", asctime(gm));
return 0;
}
listing 35
while(!kbhit()); /* wait for keypress */
listing 36
#include <stdio.h>
#include <time.h>
#include <stddef.h>
/* Print local and Greenwich mean time. */
int main(void)
{
struct tm *local, *gm;
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local time and date: %s", asctime(local));
gm = gmtime(&t);
printf("Greenwich mean time and date: %s", asctime(gm));
return 0;
}
listing 37
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm t;
t.tm_year = 90; /* year 1990 */
t.tm_mon = 1; /* month - 1 */
t.tm_mday = 7;
mktime(&t);
printf("The day of the week is %d", t.tm_wday);
return 0;
}
listing 38
struct time t;
t.ti_hour = 10;
t.ti_min = 10;
t.ti_sec = 10;
t.ti_hund = 0;
settime(&t);
listing 39
struct ftime {
unsigned ft_tsec: 5; /* seconds */
unsigned ft_min: 6; /* minutes */
unsigned ft_hour: 5; /* hours */
unsigned ft_day: 5; /* days */
unsigned ft_month: 4; /* month */
unsigned ft_year: 7; /* year from 1980 */
}
listing 40
setftime(fd, &t);
listing 41
#include <stdio.h>
#include <dos.h>
int main(void)
{
printf("hello");
sleep(10);
printf(" there");
return 0;
}
listing 42
#include <stdio.h>
#include <time.h>
int main(void)
{
char str[9];
_strtime(str);
printf("Time: %s", str);
_strdate(str);
printf(", Date: %s", str);
return 0;
}
listing 43
strftime(str, 100, "It is now %H %p", ltime)
printf(str);
listing 44
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf(asctime(ptr));
return 0;
}
listing 45
struct time t;
struct date d;
unixtodos(timeandday, &d, &t)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -