例168:在控制台打印文件
#include <stdio.h>
int main(int argc, char const *argv[])
{
FILE *fp;
char line[255];
// 打开第一个程序参数指明的文件
if ((fp = fopen(argv[1], "r")) == NULL)
printf("Error opening %s\n", argv[1]);
else
{
// 读一行打印一行
while (fgets(line, sizeof(line), fp))
fputs(line, stdout);
fclose (fp);
}
return 0;
}
例169:在控制台打印PATH变量
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
char *entry;
entry = getenv("PATH");
if (*entry)
printf("PATH=%s\n", entry);
else
printf("PATH is not defined\n");
return 0;
}
例170:在标准输出流和错误流中输出用户输入的字符串
#include <stdio.h>
int main(int argc, char const *argv[])
{
char buffer[256];
while (fgets(buffer, sizeof(buffer), stdin))
{
fputs(buffer, stdout);
fputs(buffer, stderr);
}
return 0;
}
例171:将输入字符串转为大写输出
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char line[255];
while (fgets(line, sizeof(line), stdin))
fputs(strupr(line), stdout);
return 0;
}
例172:BIOS时间(tc环境编译)
#include <stdio.h>
#include <bios.h>
int main(int argc, char const *argv[])
{
long ticks;
ticks = biostime(0, ticks);
printf("Ticks since midnight %ld\n", ticks);
_bios_timeofday(_TIME_GETCLOCK, &ticks);
printf("Seconds since midnight %f\n", ticks / 18.2);
return 0;
}
例173:计算时间(老编译器)
#include <stdio.h>
#include <time.h>
#include <dos.h>
void main (void)
{
clock_t processor_time;
printf("Processor time consumed %ld\n",
clock() / (long) CLK_TCK);
delay(2000);
printf("Processor time consumed %ld\n",
(long) clock() / (long) CLK_TCK);
delay(3000);
printf("Processor time consumed %ld\n",
clock() / (long) CLK_TCK);
}
例174:输出系统当前时间
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
time_t current_time;
time(¤t_time);
printf("The current date and time: %s", ctime(¤t_time));
return 0;
}
例175:程序延迟5秒
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
time_t current_time;
time_t start_time;
printf("About to delay 5 seconds\n");
time(&start_time);
do {
time(¤t_time);
} while ((current_time - start_time) < 5);
printf("Done\n");
return 0;
}
例176:程序延迟5秒(库函数计算时间差)
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
time_t start_time;
time_t current_time;
time(&start_time);
printf("About to delay 5 seconds\n");
do {
time(¤t_time);
// difftime计算两个时刻之间的时间差
} while (difftime(current_time, start_time) < 5.0);
printf("Done\n");
return 0;
}
例177:输出当天日期(老编译器)
#include <stdio.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
struct date curr_date;
getdate(&curr_date);
printf("Current date: %d-%d-%d\n", curr_date.da_mon, curr_date.da_day, curr_date.da_year);
return 0;
}
例178:输出当前时间
#include <stdio.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
struct time curr_time;
gettime(&curr_time);
printf("Current time %02d:%02d:%02d.%02d\n", curr_time.ti_hour, curr_time.ti_min, curr_time.ti_sec, curr_time.ti_hund);
return 0;
}
例179:输出UNIX系统时间
#include <stdio.h>
#include <dos.h>
#include <time.h>
void main (void)
{
struct time dostime;
struct date dosdate;
time_t unix_format;
struct tm *local;
getdate(&dosdate);
gettime(&dostime);
unix_format = dostounix(&dosdate, &dostime);
local = localtime(&unix_format);
printf("UNIX time: %s\n", asctime(local));
}
例180:计算时区时差
#include <stdio.h>
#include <time.h>
#include <sys\timeb.h>
int main(int argc, char const *argv[])
{
struct timeb timezone;
tzset();
ftime(&timezone);
// 1970年1月1日以来的秒数(GMT)
printf("Seconds since 1 January 1970 (GMT) %ld\n", timezone.time);
printf("Fractional seconds %d\n", timezone.millitm);
// GMT时间与本地区时差
printf("Hours difference between GMT and local zone %d\n", timezone.timezone / 60);
if (timezone.dstflag)
printf("Daylight savings time active\n");
else
printf("Daylight savings time inactive\n");
return 0;
}